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 5592a7f9..e0cb1148 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 @@ -124,8 +124,13 @@ Phase 4). and `:1210`), and its tick is the count of cycles; the timer heartbeat is not the VM's tick. Task 2a had used the kernel's timer: corrected. Each node counts its word calls; every 256th runs its cycle at the call. Test first (`test_mesh_boot.c`, 59 and 67 checks). -- [ ] The cycle's loops, in v3's order (`vm_heartbeat_run_cycle`): tick; inference every 1000 ticks; - background decay; the window's service; L8's update; the snapshot. +- [x] Background decay (v3's `vm_tick_apply_background_decay`): 64 words a cycle from a cursor down the + dictionary, so a word not called cools. The window's adaptive pass (v3's + `rolling_window_run_adaptive_pass`): diversity against the last; halve to 256, double to 4096. + Test first (`test_mesh_boot.c`: 62 checks; 70 on). +- [ ] Inference every 1000 ticks (v3's `vm_tick_inference_engine`, `inference_engine.c`). +- [ ] L8's update (v3's `vm_heartbeat_update_l8`: its metrics; the table of 64 cards, #188; the gates L1-L7). +- [ ] The 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 ### Task 6: the ACL (Phase 4's: `ACL.4th`, the word card, the countdown), and its POST diff --git a/v4/include/v4/mesh.h b/v4/include/v4/mesh.h index 9daa7ea6..9843c048 100644 --- a/v4/include/v4/mesh.h +++ b/v4/include/v4/mesh.h @@ -100,6 +100,10 @@ typedef struct { #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 */ +#define V4_MESH_WINDOW_MIN 256u /* v3's ADAPTIVE_MIN_WINDOW_SIZE */ +#define V4_MESH_SHRINK_RATE 50u /* v3's ADAPTIVE_SHRINK_RATE, percent */ +#define V4_MESH_GROWTH_THRESHOLD 5u /* v3's ADAPTIVE_GROWTH_THRESHOLD, percent */ +#define V4_MESH_DECAY_BATCH 64u /* v3's HEARTBEAT_DECAY_BATCH: words decayed in the background a cycle */ #define V4_MESH_CHECK_FREQUENCY 256u /* v3's HEARTBEAT_CHECK_FREQUENCY: word calls a heartbeat cycle */ typedef struct { uint64_t tick; /* the node's heartbeat cycles (v3's tick_count): by execution, as v3 */ @@ -116,6 +120,9 @@ typedef struct { uint32_t adaptive_check_accumulator; uint8_t is_warm; uint8_t adaptive_pending; /* the heartbeat should check the window's diversity */ + uint64_t last_pattern_diversity, diversity_checks; + v4_cell decay_cursor; /* background decay: the next word's xt, down the dictionary; 0: from LATEST */ + uint64_t decay_cycles; /* pipelining (Loop #4): the word called before, L8's gate, and the node's counts */ uint32_t prev_id; /* V4_MESH_NO_WORD: none yet */ uint8_t l4_pipelining; diff --git a/v4/system/mesh.c b/v4/system/mesh.c index 4a6c497a..2661a8fa 100644 --- a/v4/system/mesh.c +++ b/v4/system/mesh.c @@ -340,11 +340,73 @@ static void pipeline(unsigned place, v4_mesh_physics *p, uint32_t id) } #endif -/* a node's heartbeat cycle (v3's vm_heartbeat_run_cycle): its tick; the per-tick loops follow (Task 3) */ +/* background decay (v3's vm_tick_apply_background_decay): V4_MESH_DECAY_BATCH words a cycle, from where the + * last cycle stopped, down the dictionary from LATEST -- so a word not called cools too */ +static void background_decay(unsigned place, v4_mesh_physics *p) +{ + v4_fabric_node *x = v4_fabric_node_at(&the->f, place); + v4_cell c; + unsigned k; + if (!x) return; + c = p->decay_cursor; + if (!is_word(place, c)) c = x->n.mem[the->im->latest]; /* gone, or none yet: from the newest */ + for (k = 0; k < V4_MESH_DECAY_BATCH && is_word(place, c); k++) { + v4_cell id = x->n.mem[c - 4]; + if (id >= 0 && id < (v4_cell)V4_MESH_WORD_IDS && accounts_of(place)[id].xt == c) { + v4_mesh_account *a = &accounts_of(place)[id]; + decay(a, p, p->tick - a->last_decay_tick); + a->last_decay_tick = p->tick; + } + c = x->n.mem[c - 1]; + } + p->decay_cursor = is_word(place, c) ? c : 0; + p->decay_cycles++; +} + +/* the window's adaptive pass (v3's rolling_window_run_adaptive_pass), when due: its diversity -- how often a + * word differs from the one before, over the window -- against the last; little growth and it halves, + * to no less than 256; growth and it doubles, to no more than 4096 */ +static void window_service(v4_mesh_physics *p) +{ + uint64_t diversity = 0, delta, growth_q48, threshold_q48; + uint32_t limit, i, prev = 0; + int first = 1; + if (!p->adaptive_pending) return; + p->adaptive_pending = 0; + if (!p->is_warm || p->total_executions < 2) return; + limit = p->effective_window_size; + for (i = 0; i < limit; i++) { + uint32_t cur = p->history[(p->window_pos + V4_MESH_WINDOW - limit + i) % V4_MESH_WINDOW]; + if (!first && cur != prev) diversity++; + prev = cur; + first = 0; + } + p->diversity_checks++; + if (p->last_pattern_diversity == 0) { p->last_pattern_diversity = diversity; return; } + delta = diversity > p->last_pattern_diversity ? diversity - p->last_pattern_diversity : 0; + growth_q48 = (delta << 16) / p->last_pattern_diversity; + threshold_q48 = ((uint64_t)V4_MESH_GROWTH_THRESHOLD << 16) / 100u; + if (growth_q48 < threshold_q48) { + if (p->effective_window_size > V4_MESH_WINDOW_MIN) { + uint32_t n = (p->effective_window_size * V4_MESH_SHRINK_RATE) / 100u; + if (n < V4_MESH_WINDOW_MIN) n = V4_MESH_WINDOW_MIN; + if (n < p->effective_window_size) p->effective_window_size = n; + } + } else if (p->effective_window_size < V4_MESH_WINDOW) { + uint32_t n = (p->effective_window_size * ((100u * 100u) / V4_MESH_SHRINK_RATE)) / 100u; + if (n > V4_MESH_WINDOW) n = V4_MESH_WINDOW; + if (n > p->effective_window_size) p->effective_window_size = n; + } + p->last_pattern_diversity = diversity; +} + +/* a node's heartbeat cycle, in v3's order (vm_heartbeat_run_cycle): its tick (and inference, every 1000); + * background decay; the window's service; L8's update; the snapshot */ static void heartbeat_cycle(unsigned place, v4_mesh_physics *p) { - (void)place; p->tick++; + background_decay(place, p); + window_service(p); } /* at `call` (exec.h), on a word -- any other target, nothing -- in v3's order (physics_pre_execute, diff --git a/v4/tests/test_mesh_boot.c b/v4/tests/test_mesh_boot.c index 73eb0997..07f260df 100644 --- a/v4/tests/test_mesh_boot.c +++ b/v4/tests/test_mesh_boot.c @@ -207,8 +207,8 @@ int main(void) 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->last_decay_tick == 0 && a->state_flags == 0, - "a new word's physics: no heat, no temperature, never decayed, no state flags"); + 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].l3_decay == 1, "each node has v3's initial decay slope (Q48.16, 1/3) and Loop #3 on"); r = line(V4_MESH_HERA, ": IMM ; IMMEDIATE ' IMM .", &how); @@ -270,6 +270,30 @@ int main(void) "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; + 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); +#if ENABLE_PIPELINING && ENABLE_HOTWORDS_CACHE +#define Q7_HOT 51u /* speculation on a word that follows itself sets its heat to 51, as v3's */ +#else +#define Q7_HOT 90u +#endif + 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); + } r = line(V4_MESH_HERA, "FORGET P7", &how); } r = line(V4_MESH_HERA, "3 SQ .", &how);