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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 20:32:36 +00:00
parent 922451cf52
commit b880a6eaed
8 changed files with 145 additions and 7 deletions
+17 -1
View File
@@ -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);
+69 -5
View File
@@ -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];
+39
View File
@@ -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);