From b880a6eaed424507b80cdb83d6f5581cd69f79ea Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 20:32:36 +0000 Subject: [PATCH] feat(v4): Phase 7 Task 2a -- decay, heat, temperature and the rolling window at every call (#189) As v3's physics_pre_execute, in its order (Captain Bob, 2026-11-13: "Every call, as v3"): a word's heat decays by the ticks since its last touch at the node's decay slope (Q48.16, v3's arithmetic), unless frozen or L8 has Loop #3 off; +1; its temperature smoothed 3:1; its ID into the node's Rolling Window of Truth (4096, warm at 1024, a diversity check due every 512). The kernel's heartbeat is read at each call. Pipelining and the hot-words cache ruled "Built, off as v3" (Task 2b). Test first: test_mesh_boot.c, 56 checks. amd64 boots, RESULT: PASS, 146 s against Phase 2's 142 s. Not pushed (held). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm --- ...ngine-step4-phase7-the-loops-and-parity.md | 15 +++- .../runs/doe-amd64-20261113-202955.csv | 0 kernel/src/v4/sk_v4.c | 2 + logs/20261113-202955/amd64/README.md | 1 + .../amd64/qemu-amd64-20261113-202955.log | 3 + v4/include/v4/mesh.h | 18 ++++- v4/system/mesh.c | 74 +++++++++++++++++-- v4/tests/test_mesh_boot.c | 39 ++++++++++ 8 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 experiments/bare_metal/runs/doe-amd64-20261113-202955.csv create mode 100644 logs/20261113-202955/amd64/README.md create mode 100644 logs/20261113-202955/amd64/qemu-amd64-20261113-202955.log diff --git a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md index b137d6d6..2159664e 100644 --- a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md +++ b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md @@ -31,6 +31,12 @@ 5. **Mama's 20 cases** run as they are, on v4 words with v3's names mapped to v4's equivalents; **the C modules** (ACL, break_me) are rewritten for v4. Inventory and rulings: `docs/v4.0.0/POST-PARITY.md`. +6. **Pipelining and the hot-words cache: "Built, off as v3"** (2026-11-13). Asked first "Build a + hot-words cache" (in front of FIND, in F18/StarForth, as v3 promotes into its own); then it was found + that v3 as built has both off (`ENABLE_PIPELINING`, `ENABLE_HOTWORDS_CACHE`: `n` in + `Kconfig.physics` and `kernel/Makefile`), so v3's boot records no transitions and uses no cache. + Ruled: both written and tested, behind the same two build options, off by default as in v3. + ## What ruling 3 brings in v3's POST is 26 modules (`v3/src/test_runner/modules/`, about 1,050 cases; v3's bare-metal boot says @@ -84,7 +90,14 @@ Phase 4). L8's Loop #3 gate); `v4_mesh_state_flags`. Immediate and hidden are read from the node's header when asked, so they are never stale. v3's `mass_bytes` is only ever set, never read, in v3: not kept until a word reads it. -### Task 2: the per-call loops at `call` (decay on touch, window, transitions) -- measured +### Task 2a: the per-call loops at `call` -- decay on touch, heat, temperature, the rolling window -- measured +- [x] Test first (`test_mesh_boot.c`, 56 checks): heat +1 a call; decay by the ticks since at v3's + slope and arithmetic (6 ticks: 1; 3 ticks: 0); none for a frozen word, none while L8 has Loop #3 + off; the temperature; the window. +- [x] `mesh.c`'s call hook, in v3's order; the kernel's heartbeat read at each call (`ticks`). Until + Task 3 brings L8, Loop #2 and #3 are on (v3's boot sets both off and lets its card decide). +- [x] Measured: amd64 boot 146 s, against Phase 2's 142 s (about +3%). +### Task 2b: pipelining (transitions, the likely next word, speculation) and the hot-words cache in front of FIND, behind `ENABLE_PIPELINING` and `ENABLE_HOTWORDS_CACHE`, off as v3 ### Task 3: the per-tick loops (tuner, slope, background decay, L8, inference, snapshot) ### Task 4: the words that read and set them (`FREEZE-WORD`, the inference words, ...), and their POST ### Task 5: the StarForth words (Phase 3's: `init.4th`, `starforth.4th`), and their POST diff --git a/experiments/bare_metal/runs/doe-amd64-20261113-202955.csv b/experiments/bare_metal/runs/doe-amd64-20261113-202955.csv new file mode 100644 index 00000000..e69de29b diff --git a/kernel/src/v4/sk_v4.c b/kernel/src/v4/sk_v4.c index 42ea0699..d8607414 100644 --- a/kernel/src/v4/sk_v4.c +++ b/kernel/src/v4/sk_v4.c @@ -78,6 +78,7 @@ static uint32_t sk_v4_charge_ids[V4_MESH_WORD_IDS]; static uint64_t sk_v4_charge_calls[V4_MESH_WORD_IDS]; static uint32_t sk_v4_charge_n; static v4_mesh_physics sk_v4_physics[SK_V4_PLACES]; /* each node's physics: its tick, decay slope, L8's gates (#189, Phase 7) */ +static uint64_t sk_v4_heartbeat_ticks(void) { return heartbeat_ticks(); } /* what decay is measured by, as v3's VM tick */ static void sk_v4_charge_one(void *ctx, v4_cell id, v4_uheat_t calls) { (void)ctx; @@ -311,6 +312,7 @@ 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.ticks = sk_v4_heartbeat_ticks; 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; diff --git a/logs/20261113-202955/amd64/README.md b/logs/20261113-202955/amd64/README.md new file mode 100644 index 00000000..51c319ee --- /dev/null +++ b/logs/20261113-202955/amd64/README.md @@ -0,0 +1 @@ +Development boot, not acceptance evidence (2026-11-13): amd64 with #189 Phase 7 Task 2a (decay, heat, temperature and the rolling window at every call). RESULT: PASS; Stadium words promotions=141, CONSERVED; dict_hash 0xa854ba15b437d288 unchanged. The boot took 146 s (Phase 2: 142 s; about +3%). The protocol run is the evidence. diff --git a/logs/20261113-202955/amd64/qemu-amd64-20261113-202955.log b/logs/20261113-202955/amd64/qemu-amd64-20261113-202955.log new file mode 100644 index 00000000..1b772766 --- /dev/null +++ b/logs/20261113-202955/amd64/qemu-amd64-20261113-202955.log @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5175561f3b5e40cf8f32f34fee848d713cdc47bb43731194ab0beb493db9464a +size 9470 diff --git a/v4/include/v4/mesh.h b/v4/include/v4/mesh.h index 8e3f4745..8bb4a2da 100644 --- a/v4/include/v4/mesh.h +++ b/v4/include/v4/mesh.h @@ -73,10 +73,22 @@ typedef struct { /* Each node's physics (v3's VM fields): its heartbeat's tick, the decay slope (Q48.16 heat a tick; * v3's INITIAL_DECAY_SLOPE_Q48, which the inference engine tunes), and L8's gate on Loop #3. */ #define V4_MESH_INITIAL_DECAY_SLOPE_Q48 21845u +#define V4_MESH_WINDOW 4096u /* v3's ROLLING_WINDOW_SIZE */ +#define V4_MESH_WINDOW_WARM 1024u /* executions before the window is warm, as v3 */ +#define V4_MESH_ADAPTIVE_CHECK 512u /* v3's ADAPTIVE_CHECK_FREQUENCY: executions between diversity checks */ typedef struct { uint64_t tick; uint64_t decay_slope_q48; - uint8_t l3_decay; + uint8_t l3_decay; /* L8's gate on Loop #3 */ + uint8_t l2_window; /* L8's gate on Loop #2 */ + /* the Rolling Window of Truth (Loop #2; v3's RollingWindowOfTruth): the IDs of the words called, in order */ + uint32_t history[V4_MESH_WINDOW]; + uint32_t window_pos; /* where the next is written */ + uint64_t total_executions; + uint32_t effective_window_size; /* may shrink from V4_MESH_WINDOW (Loop #5) */ + uint32_t adaptive_check_accumulator; + uint8_t is_warm; + uint8_t adaptive_pending; /* the heartbeat should check the window's diversity */ } v4_mesh_physics; typedef struct v4_mesh v4_mesh; @@ -113,6 +125,7 @@ struct v4_mesh { v4_mesh_account *accounts; unsigned char *is_word; v4_mesh_physics *physics; /* `capacity` of them: each node's (#189, Phase 7) */ + uint64_t (*ticks)(void); /* the heartbeat's tick count, read at each call (0: v4_mesh_set_tick's) */ /* a word has gone, its ID free for the next: the owner forgets what it keeps by it (may be 0) */ void (*forgot)(void *self, unsigned place, v4_cell id); void *self; @@ -152,6 +165,9 @@ int v4_mesh_busy(const v4_mesh *ms); /* A word's record on the node in `place`, by its xt: 0 if that is not a word's xt. */ const v4_mesh_account *v4_mesh_account_at(const v4_mesh *ms, unsigned place, v4_cell xt); +/* The heartbeat's tick, for every node (v3's vm->heartbeat.tick_count): what decay is measured by. */ +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. */ unsigned v4_mesh_state_flags(const v4_mesh *ms, unsigned place, v4_cell xt); diff --git a/v4/system/mesh.c b/v4/system/mesh.c index ebe7ae34..d9da9308 100644 --- a/v4/system/mesh.c +++ b/v4/system/mesh.c @@ -182,14 +182,71 @@ static void words_gone(unsigned place, const v4_node *n, v4_cell w) } } -/* at `call` (exec.h): a word's record counts one; any other target, nothing */ +/* Loop #3, on touch (v3's physics_metadata_apply_linear_decay): the ticks since, times the decay slope + * (Q48.16), off the heat -- not for a frozen word, not while L8 has Loop #3 off */ +static void decay(v4_mesh_account *a, const v4_mesh_physics *p, uint64_t elapsed) +{ + uint64_t amount; + if ((a->state_flags & V4_MESH_STATE_FROZEN) || elapsed == 0 || !p->l3_decay) return; + amount = (elapsed * p->decay_slope_q48) >> 16; + if (amount == 0) return; + a->heat = amount >= a->heat ? 0 : a->heat - amount; +} + +/* the temperature (v3's physics_metadata_touch): the heat in Q8, smoothed 3:1 */ +static uint16_t temperature(v4_uheat_t heat, uint16_t prior) +{ + uint64_t scaled; + uint16_t target; + if (heat == 0) return 0; + scaled = (uint64_t)heat << 8; + target = scaled > 65535u ? 65535u : (uint16_t)scaled; + return (uint16_t)((3u * prior + target) / 4u); +} + +/* Loop #2 (v3's rolling_window_record_execution): the word's ID into the window */ +static void window_record(v4_mesh_physics *p, uint32_t id) +{ + p->history[p->window_pos] = id; + p->window_pos = (p->window_pos + 1u) % V4_MESH_WINDOW; + p->total_executions++; + if (p->total_executions >= V4_MESH_WINDOW_WARM && !p->is_warm) p->is_warm = 1; + if (p->is_warm && ++p->adaptive_check_accumulator >= V4_MESH_ADAPTIVE_CHECK) { + p->adaptive_check_accumulator = 0; + p->adaptive_pending = 1; + } +} + +/* at `call` (exec.h), on a word -- any other target, nothing -- in v3's order (physics_pre_execute, + * V3-PARITY.md 2.1; Captain Bob, 2026-11-13: "Every call, as v3"): decay by the ticks since, heat +1, + * the count the Stadium is charged by, the temperature, the rolling window */ static void on_call(void *ctx, v4_node *n, v4_cell target) { unsigned place = place_of_device(ctx); v4_cell id; + v4_mesh_account *a; if (!is_word(place, target)) return; id = n->mem[target - 4]; - if (id >= 0 && id < (v4_cell)V4_MESH_WORD_IDS) { accounts_of(place)[id].calls++; accounts_of(place)[id].since++; } + if (id < 0 || id >= (v4_cell)V4_MESH_WORD_IDS) return; + a = &accounts_of(place)[id]; + if (the->physics) { + v4_mesh_physics *p = &the->physics[place]; + if (the->ticks) p->tick = the->ticks(); + decay(a, p, p->tick - a->last_decay_tick); + a->last_decay_tick = p->tick; + a->heat++; + a->temperature_q8 = temperature(a->heat, a->temperature_q8); + if (p->l2_window) window_record(p, (uint32_t)id); + } + a->calls++; + a->since++; +} + +void v4_mesh_set_tick(v4_mesh *ms, uint64_t tick) +{ + unsigned k; + if (!ms->physics) return; + for (k = 0; k < ms->capacity; k++) ms->physics[k].tick = tick; } /* is the entry at xt named `name`? (dict.f18: the name's word address at xt-2, its count byte, then its @@ -228,9 +285,16 @@ static void words_attach(unsigned place, v4_fabric_node *x) x->n.mem[im->word_forgotten] = gone; } if (the->physics) { /* the node's physics, as v3's VM starts */ - the->physics[place].tick = 0; - the->physics[place].decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48; - the->physics[place].l3_decay = 1; + v4_mesh_physics *p = &the->physics[place]; + uint64_t tick = p->tick; /* the heartbeat's, which goes on */ + unsigned char *z = (unsigned char *)p; + unsigned long i; + for (i = 0; i < sizeof *p; i++) z[i] = 0; + p->tick = tick; + p->decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48; + p->l3_decay = 1; + p->l2_window = 1; /* v3: "No config yet: recorded" */ + p->effective_window_size = V4_MESH_WINDOW; } x->es.on_call = on_call; x->es.on_call_ctx = &the->kernel[place]; diff --git a/v4/tests/test_mesh_boot.c b/v4/tests/test_mesh_boot.c index 94f761c8..45d339d7 100644 --- a/v4/tests/test_mesh_boot.c +++ b/v4/tests/test_mesh_boot.c @@ -199,6 +199,45 @@ int main(void) 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; + r = line(V4_MESH_HERA, ": P7 DUP * ; ' P7 .", &how); + a = v4_mesh_account_at(&ms, 0, (v4_cell)strtol(r, 0, 10)); + 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); + 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);