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
+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,