feat(v4): Phase 7 Task 3 -- background decay and the window's adaptive pass in each node's heartbeat cycle (#189)

In v3's order (vm_heartbeat_run_cycle): the tick; background decay -- 64 words a cycle from a cursor down
the dictionary (v3's vm_tick_apply_background_decay), so a word not called cools; the window's adaptive
pass on its diversity (v3's rolling_window_run_adaptive_pass: halve to 256, double to 4096). Test first:
test_mesh_boot.c, 62 checks (70 with pipelining and the cache on). Inference, L8 and the snapshot next.
Not pushed (held to the end of Phase 7).

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 22:20:22 +00:00
parent 5c79bddbbb
commit 7c33b99ebe
4 changed files with 104 additions and 6 deletions
+7
View File
@@ -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;
+64 -2
View File
@@ -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,
+26 -2
View File
@@ -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);