interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / runtime / rx_plan_reuse.c

1351 lines · 61943 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * OMEGA_PLAN_REUSE -- plan IR and verified plan cache (spec/plan-reuse.md).
19 *
20 * Every instance has its own World and its own native Aienux authority. The
21 * test plays the outside: it holds the admin (to give the principal its
22 * authority, and to revoke it), builds the World an instance starts from,
23 * and publishes run tokens. Cognition (goal shape, retrieval, applicability,
24 * binding, or AIEN's search) and execution (the plan as an action graph on
25 * resident reactions) are measured separately.
26 *
27 * For every repeated-workload instance the first-use path (cache off) runs
28 * in a mirror World built identically; its outcome is compared with the
29 * reuse path's.
30 */
31#include "runtime/aienos_cap.h"
32#include "runtime/rx_plan.h"
33#include "runtime/rx_plan_arrange.h"
34#include "runtime/rx_world.h"
35#include "omega_evidence.h"
36#include "sha256.h"
37
38#include <dirent.h>
39#include <linux/perf_event.h>
40#include <sched.h>
41#include <signal.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sys/ioctl.h>
46#include <sys/syscall.h>
47#include <sys/utsname.h>
48#include <time.h>
49#include <unistd.h>
50
51enum { SUBJ_EXTERNAL = 100, ISSUER = 3, SUBJ_PLAN = 71 };
52
53#define RES_CELL 0xB000001ull
54#define RES_RUN 0xB000002ull
55#define RES_DECISION 0xB000003ull
56#define RES_INDEX 0xB000004ull
57#define RES_ENV 0xB000005ull
58#define RES_UNIT 0xB000100ull
59
60#define WORLD_GEN 7u
61#define COG_GEN 3u
62#define N_REPEAT 40u
63#define N_ADV 10u
64#define MAX_EXPAND 2000000u
65
66static int g_checks, g_fail;
67
68#define CHECK(cond, ...) do { \
69 g_checks++; \
70 if (!(cond)) { \
71 g_fail++; \
72 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \
73 fprintf(stderr, __VA_ARGS__); \
74 fputc('\n', stderr); \
75 } \
76 } while (0)
77
78static uint64_t now_ns(void) {
79 struct timespec ts;
80 clock_gettime(CLOCK_MONOTONIC, &ts);
81 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
82}
83
84static uint64_t cpu_ns(void) {
85 struct timespec ts;
86 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
87 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
88}
89
90/* ---- instructions retired (user space, this thread) ---- */
91
92static int g_perf = -1;
93
94static void perf_open(void) {
95 struct perf_event_attr a;
96 memset(&a, 0, sizeof a);
97 a.type = PERF_TYPE_HARDWARE;
98 a.size = sizeof a;
99 a.config = PERF_COUNT_HW_INSTRUCTIONS;
100 a.disabled = 1;
101 a.exclude_kernel = 1;
102 a.exclude_hv = 1;
103 g_perf = (int)syscall(SYS_perf_event_open, &a, 0, -1, -1, 0);
104 if (g_perf >= 0) ioctl(g_perf, PERF_EVENT_IOC_ENABLE, 0);
105}
106
107static uint64_t instr(void) {
108 uint64_t v = 0;
109 if (g_perf < 0 || read(g_perf, &v, sizeof v) != (ssize_t)sizeof v) return 0;
110 return v;
111}
112
113/* ---- receipt figures ---- */
114
115typedef struct {
116 uint32_t n;
117 uint64_t ops[N_REPEAT], cpu[N_REPEAT], wall[N_REPEAT], ins[N_REPEAT], e2e[N_REPEAT];
118 uint64_t exec[N_REPEAT];
119 uint64_t compile[N_REPEAT], run[N_REPEAT], verify[N_REPEAT];
120 uint64_t ident[N_REPEAT]; /* first use: CPU spent on the template identity hash */
121} Series;
122
123static struct {
124 char cpus[64];
125 uint64_t core_class;
126 Series first[2], reuse[2];
127 uint32_t reuse_accepted[2], reuse_success[2], outcome_equal[2], evidence_equal[2];
128 uint32_t cold_success[2], moves[2], evidence_words[2], nodes[2];
129 uint32_t semantic_ids_equal[2], realizations_distinct[2];
130 uint32_t candidate_reference_runs, promotions;
131 uint32_t adv_cases, adv_refused, adv_axis_match, adv_decision_crumbs;
132 uint32_t adv_by_axis[PL_AX_COUNT], adv_refused_by_axis[PL_AX_COUNT];
133 uint32_t blind_by_axis[PL_AX_COUNT], blind_ok_by_axis[PL_AX_COUNT];
134 uint32_t blind_violation_by_axis[PL_AX_COUNT];
135 uint32_t accepted_total, false_applicable;
136 uint32_t id_roundtrip, id_order_invariant, id_mutations, id_mutations_changed;
137 uint32_t id_nonsemantic, id_nonsemantic_same, rederived_same_id, rederived_then_reused;
138 uint32_t adapt_n, adapt_ok, adapt_then_reuse, adapt_steps, adapt_cold_steps;
139 uint64_t adapt_ops_med, adapt_cold_ops_med, adapt_cpu_med, adapt_cold_cpu_med;
140 int energy_ok;
141 uint64_t energy_reuse_refused;
142 char energy_why[160];
143 double idle_w, cold_pkg_mj_per, reuse_pkg_mj_per, cold_cpup_mj_per, reuse_cpup_mj_per;
144 uint64_t cold_loops, reuse_loops, loop_ns;
145 int perf_ok;
146 uint64_t stored, dedup;
147} R;
148
149/* ---- environment ---- */
150
151enum { SHAPE_REVERSE = 0, SHAPE_MERGE = 1, SHAPE_OTHER = 2 };
152
153typedef enum {
154 PERT_NONE = 0, PERT_BINDING, PERT_GOAL, PERT_WORLD_GEN, PERT_COG_GEN, PERT_ENV,
155 PERT_STATE_SWAP, PERT_STATE_OUTSIDER, PERT_AUTHORITY, PERT_RESOURCES
156} Pert;
157
158typedef struct {
159 uint32_t shape, seed;
160 Pert pert;
161 uint64_t world_gen, cog_gen, max_steps;
162} Spec;
163
164typedef struct {
165 AienosCapAdmin *admin;
166 AienosCapView *view;
167 RxWorld w;
168 PlEnv env;
169 RxObjRef decision, index;
170 uint32_t n_units, n_goal_units;
171 RxObjRef unit[PLA_MAX_UNITS]; /* role order: goal units, then distractors */
172 RxCapRef unit_cap[PLA_MAX_UNITS];
173 RxCapRef ext_unit[PLA_MAX_UNITS];
174 RxCapRef ext_run, ext_decision, ext_index;
175 AgCapTable caps;
176 PlExecCtx x;
177 PlRunStore rs;
178 uint64_t seq;
179} Env;
180
181static AgSkillTable g_skills;
182
183static RxCapRef mint(Env *e, uint32_t subject, uint64_t resource, uint32_t rights) {
184 AienosCapRef office;
185 aienos_cap_office(e->admin, &office);
186 AienosCapMint m = { ISSUER, subject, resource, rights, 0, { UINT32_MAX, 0 }, office };
187 AienosCapRef r = { UINT32_MAX, 0 };
188 if (aienos_cap_mint(e->admin, &m, &r) != 0) r = (AienosCapRef){ UINT32_MAX, 0 };
189 return (RxCapRef){ r.cap_id, r.generation };
190}
191
192static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s >> 8; }
193
194static int put_unit(Env *e, uint32_t k, const RxObjRef *on) {
195 RxMutation m[2] = { { e->unit[k], PLA_F_SUPPORT, on ? pl_ref_pack(*on) : 0 },
196 { e->unit[k], PLA_F_SELF, pl_ref_pack(e->unit[k]) } };
197 return rx_world_publish_external(&e->w, e->ext_unit[k], m, 2) > 0 ? 0 : -1;
198}
199
200/* Goal units per shape, bottom to top of the start, and goal facts. */
201static uint32_t goal_units(uint32_t shape) { return shape == SHAPE_OTHER ? 5 : 6; }
202
203static int env_build(Env *e, const Spec *sp) {
204 memset(e, 0, sizeof *e);
205 if (aienos_cap_start(&e->admin, &e->view) != 0) return -1;
206 if (rx_world_init_native(&e->w, e->view, 4, 1u << 16) != RX_OK) return -1;
207 e->w.external_subject = SUBJ_EXTERNAL;
208 const uint32_t R_ = RX_RIGHT_READ, W = RX_RIGHT_WRITE, RW = R_ | W;
209 uint32_t seed = sp->seed * 2654435761u + 17u;
210
211 /* Vary object identities: fillers, some retired (their slots come back
212 * with a new generation). */
213 uint32_t nf = 1 + lcg(&seed) % 9;
214 RxObjRef fill[16];
215 for (uint32_t i = 0; i < nf; i++)
216 if (rx_world_create(&e->w, 0x5B0F, RX_PERSIST_EPHEMERAL, RES_ENV, NULL, &fill[i]) != RX_OK)
217 return -1;
218 for (uint32_t i = 0; i < nf; i += 2) rx_world_retire(&e->w, fill[i]);
219
220 e->n_goal_units = goal_units(sp->shape);
221 e->n_units = e->n_goal_units + 4;
222 /* Create units in a seed-dependent order. */
223 uint32_t order[PLA_MAX_UNITS];
224 for (uint32_t k = 0; k < e->n_units; k++) order[k] = k;
225 for (uint32_t k = e->n_units; k > 1; k--) {
226 uint32_t j = lcg(&seed) % k, t = order[k - 1];
227 order[k - 1] = order[j];
228 order[j] = t;
229 }
230 for (uint32_t i = 0; i < e->n_units; i++) {
231 uint32_t k = order[i];
232 uint64_t f[RX_MAX_FIELDS] = { 0, 0x100u + k };
233 if (rx_world_create(&e->w, PL_OT_UNIT, RX_PERSIST_RESIDENT, RES_UNIT + k, f, &e->unit[k]) != RX_OK)
234 return -1;
235 }
236 for (uint32_t k = 0; k < e->n_units; k++) e->ext_unit[k] = mint(e, SUBJ_EXTERNAL, RES_UNIT + k, W);
237
238 /* Start arrangement. */
239 const RxObjRef *U = e->unit;
240 uint32_t g = e->n_goal_units;
241 if (sp->shape == SHAPE_MERGE) {
242 /* P = 0,1,2 and Q = 3,4,5, two towers of three. */
243 put_unit(e, 0, NULL); put_unit(e, 1, &U[0]); put_unit(e, 2, &U[1]);
244 put_unit(e, 3, NULL); put_unit(e, 4, &U[3]); put_unit(e, 5, &U[4]);
245 } else {
246 put_unit(e, 0, NULL);
247 for (uint32_t k = 1; k < g; k++) put_unit(e, k, &U[k - 1]);
248 }
249 /* Distractors: one tower of two, two alone. */
250 put_unit(e, g, NULL); put_unit(e, g + 1, &U[g]); put_unit(e, g + 2, NULL); put_unit(e, g + 3, NULL);
251 uint32_t top = sp->shape == SHAPE_MERGE ? 2 : g - 1;
252 if (sp->pert == PERT_STATE_SWAP) {
253 /* The top two of the (first) tower change places. */
254 put_unit(e, top, &U[top - 2]);
255 put_unit(e, top - 1, &U[top]);
256 }
257 if (sp->pert == PERT_STATE_OUTSIDER) put_unit(e, g + 2, &U[top]);
258
259 /* Goal facts. */
260 uint64_t facts[PLA_MAX_FACTS] = { 0 };
261 uint32_t nfct = 0;
262 if (sp->shape == SHAPE_MERGE) {
263 /* One tower P0 Q0 P1 Q1 P2 Q2 from the floor up. */
264 uint32_t seqv[6] = { 0, 3, 1, 4, 2, 5 };
265 facts[nfct++] = pla_fact(U[seqv[0]], NULL);
266 for (uint32_t i = 1; i < 6; i++) facts[nfct++] = pla_fact(U[seqv[i]], &U[seqv[i - 1]]);
267 } else {
268 /* Reverse the tower. */
269 facts[nfct++] = pla_fact(U[g - 1], NULL);
270 for (uint32_t k = 0; k + 1 < g; k++) facts[nfct++] = pla_fact(U[k], &U[k + 1]);
271 }
272 for (uint32_t k = nfct; k > 1; k--) { /* listing order is not meaning */
273 uint32_t j = lcg(&seed) % k;
274 uint64_t t = facts[k - 1];
275 facts[k - 1] = facts[j];
276 facts[j] = t;
277 }
278 if (sp->pert == PERT_BINDING) {
279 /* The goal names a unit that is then retired; a replacement takes its
280 * place (same slot id, next generation). */
281 uint32_t k = g - 1;
282 RxObjRef old = U[k];
283 uint64_t f[RX_MAX_FIELDS] = { 0, 0x100u + k };
284 if (rx_world_retire(&e->w, old) != RX_OK ||
285 rx_world_create(&e->w, PL_OT_UNIT, RX_PERSIST_RESIDENT, RES_UNIT + k, f, &e->unit[k]) != RX_OK)
286 return -1;
287 put_unit(e, k, sp->shape == SHAPE_MERGE ? &U[k - 1] : &U[k - 1]);
288 }
289
290 uint64_t z[RX_MAX_FIELDS] = { 0 };
291 uint64_t wg[RX_MAX_FIELDS] = { sp->world_gen };
292 uint64_t cg[RX_MAX_FIELDS] = { sp->cog_gen, 0xA1E0 };
293 uint64_t st[RX_MAX_FIELDS] = { sp->pert == PERT_ENV ? 0u : 1u };
294 RxObjRef fr;
295 if (rx_world_create(&e->w, PL_OT_WORLDGEN, RX_PERSIST_RESIDENT, RES_ENV, wg, &e->env.obj[PL_ENV_WORLDGEN]) != RX_OK ||
296 rx_world_create(&e->w, PL_OT_COGNITION, RX_PERSIST_RESIDENT, RES_ENV, cg, &e->env.obj[PL_ENV_COGNITION]) != RX_OK ||
297 rx_world_create(&e->w, PL_OT_STATION, RX_PERSIST_RESIDENT, RES_ENV, st, &e->env.obj[PL_ENV_STATION]) != RX_OK ||
298 rx_world_create(&e->w, PL_OT_GOALFACTS, RX_PERSIST_RESIDENT, RES_ENV, facts, &fr) != RX_OK)
299 return -1;
300 uint64_t gl[RX_MAX_FIELDS] = { 1000u + sp->seed, PLA_GOAL_ARRANGE, sp->max_steps, 1000, pl_ref_pack(fr) };
301 if (rx_world_create(&e->w, PL_OT_GOAL, RX_PERSIST_RESIDENT, RES_ENV, gl, &e->env.obj[PL_ENV_GOAL]) != RX_OK ||
302 rx_world_create(&e->w, PL_OT_DECISION, RX_PERSIST_RESIDENT, RES_DECISION, z, &e->decision) != RX_OK ||
303 rx_world_create(&e->w, PL_OT_INDEX, RX_PERSIST_RESIDENT, RES_INDEX, z, &e->index) != RX_OK)
304 return -1;
305 e->env.obj[PL_ENV_FACTS] = fr;
306
307 e->caps.subject = SUBJ_PLAN;
308 for (uint32_t k = 0; k < e->n_units; k++) {
309 e->unit_cap[k] = mint(e, SUBJ_PLAN, RES_UNIT + k, RW);
310 e->caps.cap[e->caps.n].ref = e->unit_cap[k];
311 e->caps.cap[e->caps.n].resource = RES_UNIT + k;
312 e->caps.cap[e->caps.n].rights = RW;
313 e->caps.n++;
314 }
315 e->ext_run = mint(e, SUBJ_EXTERNAL, RES_RUN, W);
316 e->ext_decision = mint(e, SUBJ_EXTERNAL, RES_DECISION, W);
317 e->ext_index = mint(e, SUBJ_EXTERNAL, RES_INDEX, W);
318 e->x.cell_cap = mint(e, SUBJ_PLAN, RES_CELL, RW);
319 e->x.run_cap = mint(e, SUBJ_PLAN, RES_RUN, R_);
320 e->x.ext_run_cap = e->ext_run;
321 e->x.cell_res = RES_CELL;
322 e->x.run_res = RES_RUN;
323 e->x.skills = &g_skills;
324 e->x.core_class = R.core_class;
325
326 if (sp->pert == PERT_AUTHORITY) {
327 AienosCapRef office;
328 aienos_cap_office(e->admin, &office);
329 uint32_t k = 1;
330 aienos_cap_revoke(e->admin, office, (AienosCapRef){ e->unit_cap[k].cap_id, e->unit_cap[k].generation });
331 }
332 if (sp->pert == PERT_RESOURCES) {
333 RxResourceBudget b = e->w.budget;
334 b.energy_budget = 1;
335 rx_world_set_resources(&e->w, &b);
336 }
337 rx_world_wait_quiescent(&e->w, 30000);
338 return 0;
339}
340
341static void env_stop(Env *e) {
342 rx_world_wait_quiescent(&e->w, 30000);
343 rx_world_destroy(&e->w);
344 aienos_cap_stop(e->admin, e->view);
345}
346
347static Spec spec(uint32_t shape, uint32_t seed, Pert p) {
348 return (Spec){ shape, seed, p, WORLD_GEN, COG_GEN, 32 };
349}
350
351/* ---- one goal ---- */
352
353enum { PATH_FIRST = 1, PATH_REUSE, PATH_ADAPT, PATH_REFUSED };
354
355/* Every candidate checked and refused: the axis its furthest binding reached. */
356typedef struct {
357 uint32_t n;
358 PlanTemplate *t[PL_MAX_TEMPLATES];
359 PlAxis ax[PL_MAX_TEMPLATES];
360 int pred[PL_MAX_TEMPLATES];
361 RxObjRef slots[PL_MAX_TEMPLATES][PL_MAX_SLOTS];
362 int64_t crumb[PL_MAX_TEMPLATES];
363} Refusals;
364
365typedef struct {
366 int path;
367 PlAxis axis;
368 int pred;
369 int64_t decision_crumb;
370 PlCost cost;
371 uint64_t cog_ns, cog_cpu, cog_ins, e2e_ns;
372 int plan_rc, gen_rc, executed;
373 uint32_t prefix_moves;
374 uint64_t identify_cpu;
375 Refusals rf;
376 PlExecResult ex;
377 uint32_t status_before, moves;
378 uint8_t semantic_id[32];
379 PlanTemplate *t; /* cache entry used or stored (NULL for cold) */
380 RxObjRef slots[PL_MAX_SLOTS];
381} Trial;
382
383static PlView g_view, g_view2;
384static PlanTemplate g_tmp, g_tmp2;
385
386/* Retrieval + applicability + binding. Returns the template to run or NULL;
387 * *refused receives the candidate that got furthest. */
388/* Retrieval + binding + applicability. Returns the template to run (slots
389 * filled) or NULL. rf lists every candidate refused before the choice. */
390static PlanTemplate *decide(Env *e, PlCache *c, const PlView *v, const PlaGoal *g, RxObjRef *slots,
391 PlCost *cost, Refusals *rf) {
392 PlanTemplate *cand[PL_MAX_TEMPLATES];
393 uint32_t n = rx_plan_cache_retrieve(c, (uint32_t)g->kind, g->shape, cand, PL_MAX_TEMPLATES, cost);
394 rf->n = 0;
395 RxObjRef s[PL_MAX_SLOTS];
396 for (uint32_t i = 0; i < n; i++) {
397 uint32_t k = rf->n;
398 int seen = 0;
399 for (uint32_t attempt = 0;; attempt++) {
400 int rc = pla_bind(cand[i], v, g, attempt, s, cost);
401 if (rc == 0) break;
402 PlAxis ax = PL_AX_BINDING;
403 int pr = -1;
404 if (rc > 0) ax = rx_plan_applicable(cand[i], &e->w, v, &e->env, s, &e->caps, cost, &pr);
405 if (ax == PL_AX_OK) {
406 memcpy(slots, s, sizeof s);
407 return cand[i];
408 }
409 if (!seen || ax > rf->ax[k]) {
410 rf->t[k] = cand[i];
411 rf->ax[k] = ax;
412 rf->pred[k] = pr;
413 memcpy(rf->slots[k], s, sizeof s);
414 seen = 1;
415 }
416 }
417 if (seen) {
418 cand[i]->refusals[rf->ax[k]]++;
419 rf->n++;
420 }
421 }
422 return NULL;
423}
424
425static PlAxis furthest(const Refusals *rf) {
426 PlAxis a = PL_AX_SHAPE;
427 for (uint32_t i = 0; i < rf->n; i++)
428 if (rf->ax[i] > a) a = rf->ax[i];
429 return a;
430}
431
432/* One World decision per refused candidate (or one SHAPE refusal when there
433 * was none), then the accept. Returns the last crumb. */
434static int64_t record(Env *e, uint64_t goal_seq, Refusals *rf, const PlanTemplate *use) {
435 int64_t last = 0;
436 for (uint32_t i = 0; i < rf->n; i++)
437 last = rf->crumb[i] = rx_plan_decide(&e->w, e->ext_decision, e->decision, goal_seq, rf->ax[i],
438 rf->t[i], rf->pred[i], ++e->seq, NULL);
439 if (!rf->n && !use)
440 last = rx_plan_decide(&e->w, e->ext_decision, e->decision, goal_seq, PL_AX_SHAPE, NULL, -1,
441 ++e->seq, NULL);
442 if (use)
443 last = rx_plan_decide(&e->w, e->ext_decision, e->decision, goal_seq, PL_AX_OK, use, -1,
444 ++e->seq, NULL);
445 return last;
446}
447
448enum { MODE_COLD = 0, MODE_WARM = 1, MODE_WARM_ADAPT = 2, MODE_DECIDE_ONLY = 3 };
449
450static void cog_start(Trial *t, uint64_t *w0, uint64_t *c0, uint64_t *i0) {
451 (void)t;
452 *w0 = now_ns();
453 *c0 = cpu_ns();
454 *i0 = instr();
455}
456
457static void cog_end(Trial *t, uint64_t w0, uint64_t c0, uint64_t i0) {
458 t->cog_ins = instr() - i0;
459 t->cog_cpu = cpu_ns() - c0;
460 t->cog_ns = now_ns() - w0;
461}
462
463static int solve(Env *e, PlCache *c, int mode, Trial *t) {
464 memset(t, 0, sizeof *t);
465 uint64_t e0 = now_ns(), w0, c0, i0;
466 cog_start(t, &w0, &c0, &i0);
467 rx_plan_view(&e->w, &g_view);
468 PlaGoal g;
469 if (pla_goal_read(&g_view, &e->env, &g, &t->cost) != 0) return -1;
470 PlanTemplate *use = NULL;
471 Refusals *rf = &t->rf;
472 rf->n = 0;
473 if (mode != MODE_COLD) {
474 use = decide(e, c, &g_view, &g, t->slots, &t->cost, rf);
475 t->axis = use ? PL_AX_OK : furthest(rf);
476 if (use) {
477 t->path = PATH_REUSE;
478 t->status_before = use->status;
479 memcpy(t->semantic_id, use->semantic_id, 32);
480 }
481 }
482 const PlanTemplate *run = use;
483 if (!use && mode == MODE_DECIDE_ONLY) {
484 cog_end(t, w0, c0, i0);
485 t->path = PATH_REFUSED;
486 t->decision_crumb = record(e, g.seq, rf, NULL);
487 t->e2e_ns = now_ns() - e0;
488 return 0;
489 }
490 if (!use) {
491 PlaTarget tg;
492 PlaPlan plan;
493 const uint8_t *parent = NULL;
494 uint32_t origin = PL_ORIGIN_SEARCH;
495 int adapted = 0;
496 if (mode == MODE_WARM_ADAPT) {
497 /* For each candidate refused only on the World state it needs:
498 * reach that state, then run it. Keep the shortest whole plan. */
499 for (uint32_t i = 0; i < rf->n; i++) {
500 if (rf->ax[i] != PL_AX_STATE) continue;
501 PlaTarget tt;
502 PlaPlan prefix, rest;
503 if (pla_target_from_template(rf->t[i], rf->slots[i], &tt) != 0) continue;
504 if (pla_plan(&g_view, &g, &tt, MAX_EXPAND, &prefix, &t->cost) != 0) continue;
505 if (pla_template_moves(rf->t[i], rf->slots[i], &rest) != 0) continue;
506 if (prefix.n + rest.n > PLA_MAX_MOVES || (adapted && prefix.n + rest.n >= plan.n)) continue;
507 plan = prefix;
508 t->prefix_moves = prefix.n;
509 for (uint32_t k = 0; k < rest.n; k++) {
510 plan.mover[plan.n] = rest.mover[k];
511 plan.dest[plan.n] = rest.dest[k];
512 plan.to_floor[plan.n++] = rest.to_floor[k];
513 }
514 adapted = 1;
515 origin = PL_ORIGIN_ADAPTED;
516 parent = rf->t[i]->semantic_id;
517 }
518 }
519 if (!adapted) {
520 pla_target_from_goal(&g, &tg);
521 t->plan_rc = pla_plan(&g_view, &g, &tg, MAX_EXPAND, &plan, &t->cost);
522 if (t->plan_rc != 0) { cog_end(t, w0, c0, i0); return -2; }
523 }
524 const uint64_t *cf = g_view.o[e->env.obj[PL_ENV_COGNITION].id].field;
525 t->gen_rc = pla_generalize(&g_view, &g, &plan, origin, parent, cf[0], &g_tmp, t->slots);
526 if (t->gen_rc != 0) { cog_end(t, w0, c0, i0); return -3; }
527 t->path = adapted ? PATH_ADAPT : PATH_FIRST;
528 memcpy(t->semantic_id, g_tmp.semantic_id, 32);
529 run = &g_tmp;
530 }
531 cog_end(t, w0, c0, i0);
532 if (mode != MODE_COLD) t->decision_crumb = record(e, g.seq, rf, use);
533 if (run == &g_tmp) {
534 /* What the identity hash alone costs (a cache-free system would not compute it). */
535 g_tmp2 = g_tmp;
536 uint64_t k0 = cpu_ns();
537 rx_plan_identify(&g_tmp2);
538 t->identify_cpu = cpu_ns() - k0;
539 }
540 e->x.reference_first = use && use->status == PL_STATUS_CANDIDATE;
541 rx_plan_execute(run, &e->w, &e->env, t->slots, &e->caps, &e->x, 1, &e->rs, pla_legal, &t->ex);
542 t->executed = 1;
543 t->moves = run->n_steps;
544 if (use) {
545 rx_plan_record_use(use, t->ex.success);
546 t->t = use;
547 } else if (mode != MODE_COLD && t->ex.success) {
548 rx_plan_view(&e->w, &g_view2);
549 uint64_t wg = g_view2.o[e->env.obj[PL_ENV_WORLDGEN].id].field[0];
550 uint64_t cg = g_view2.o[e->env.obj[PL_ENV_COGNITION].id].field[0];
551 t->t = rx_plan_cache_store(c, &g_tmp, wg, cg);
552 }
553 if (t->t) rx_plan_publish_index(&e->w, e->ext_index, e->index, c, t->t);
554 t->e2e_ns = now_ns() - e0;
555 return 0;
556}
557
558/* ---- statistics ---- */
559
560static int cmp_u64(const void *a, const void *b) {
561 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b;
562 return x < y ? -1 : x > y;
563}
564
565static uint64_t median(const uint64_t *v, uint32_t n) {
566 if (!n) return 0;
567 uint64_t *c = malloc(n * sizeof *c);
568 memcpy(c, v, n * sizeof *c);
569 qsort(c, n, sizeof *c, cmp_u64);
570 uint64_t m = n % 2 ? c[n / 2] : (c[n / 2 - 1] + c[n / 2]) / 2;
571 free(c);
572 return m;
573}
574
575static void add_series(Series *s, const Trial *t) {
576 uint32_t i = s->n++;
577 s->ops[i] = pl_cost_ops(&t->cost);
578 s->cpu[i] = t->cog_cpu;
579 s->wall[i] = t->cog_ns;
580 s->ins[i] = t->cog_ins;
581 s->e2e[i] = t->e2e_ns;
582 s->exec[i] = t->ex.compile_ns + t->ex.reference_ns + t->ex.run_ns + t->ex.verify_ns;
583 s->compile[i] = t->ex.compile_ns;
584 s->run[i] = t->ex.run_ns;
585 s->verify[i] = t->ex.verify_ns;
586 s->ident[i] = t->identify_cpu;
587}
588
589static int goal_holds(Env *e) {
590 PlaGoal g;
591 rx_plan_view(&e->w, &g_view2);
592 if (pla_goal_read(&g_view2, &e->env, &g, NULL) != 0) return 0;
593 for (uint32_t i = 0; i < g.n_facts; i++) {
594 uint64_t f = g_view2.o[g.x[i].id].field[PLA_F_SUPPORT];
595 uint64_t want = g.floor[i] ? 0 : pl_ref_pack((RxObjRef){ g.t[i].id, g_view2.o[g.t[i].id].generation });
596 if (f != want) return 0;
597 }
598 return pla_legal(&g_view2);
599}
600
601/* ---- phases ---- */
602
603static PlCache g_cache;
604static Env *g_a, *g_b;
605
606static void dump_cache(const char *when) {
607 if (!getenv("PLAN_REUSE_DEBUG")) return;
608 fprintf(stderr, " cache %s: %u templates\n", when, g_cache.n);
609 for (uint32_t i = 0; i < g_cache.n; i++) {
610 const PlanTemplate *t = g_cache.t[i];
611 fprintf(stderr, " [%u] id %016llx shape %016llx steps %u slots %u/%u origin %u status %s wg %llu uses %llu\n", i,
612 (unsigned long long)pl_word(t->semantic_id, 0), (unsigned long long)pl_word(t->goal_shape, 0),
613 t->n_steps, t->n_goal_slots, t->n_slots, t->ancestry.origin, rx_plan_status_name(t->status),
614 (unsigned long long)t->verified_world_gen, (unsigned long long)t->uses);
615 }
616}
617
618static void t_repeated(uint32_t shape) {
619 printf("[*] repeated workload: %s, %u instances\n",
620 shape == SHAPE_MERGE ? "merge two towers" : "reverse a tower", N_REPEAT);
621 uint8_t first_id[32];
622 uint8_t real[N_REPEAT][32];
623 uint32_t n_real = 0;
624 R.semantic_ids_equal[shape] = 1;
625 for (uint32_t i = 0; i < N_REPEAT; i++) {
626 Spec sp = spec(shape, 100 * shape + i, PERT_NONE);
627 Trial cold, warm;
628 CHECK(env_build(g_a, &sp) == 0, "build cold %u", i);
629 int rc = solve(g_a, NULL, MODE_COLD, &cold);
630 CHECK(rc == 0 && cold.ex.success, "cold %u rc %d plan %d gen %d success %d fail %u", i, rc,
631 cold.plan_rc, cold.gen_rc, cold.ex.success, cold.ex.fail);
632 int cold_goal = goal_holds(g_a);
633 CHECK(cold_goal, "cold goal %u", i);
634 R.cold_success[shape] += cold.ex.success && cold_goal;
635 if (i == 0) memcpy(first_id, cold.semantic_id, 32);
636 else if (memcmp(first_id, cold.semantic_id, 32) != 0) R.semantic_ids_equal[shape] = 0;
637 add_series(&R.first[shape], &cold);
638 env_stop(g_a);
639
640 CHECK(env_build(g_b, &sp) == 0, "build warm %u", i);
641 rc = solve(g_b, &g_cache, MODE_WARM, &warm);
642 int warm_goal = goal_holds(g_b);
643 CHECK(rc == 0 && warm.ex.success && warm_goal, "warm %u rc %d path %d axis %s success %d fail %u",
644 i, rc, warm.path, rx_plan_axis_name(warm.axis), warm.ex.success, warm.ex.fail);
645 CHECK(warm.decision_crumb > 0 && rx_world_crumb(&g_b->w, (uint64_t)warm.decision_crumb),
646 "decision crumb %u", i);
647 if (i == 0) {
648 CHECK(warm.path == PATH_FIRST && warm.t && warm.t->status == PL_STATUS_CANDIDATE,
649 "first instance stores a CANDIDATE");
650 } else {
651 CHECK(warm.path == PATH_REUSE, "instance %u reused (axis %s)", i, rx_plan_axis_name(warm.axis));
652 if (warm.path == PATH_REUSE) {
653 R.reuse_accepted[shape]++;
654 R.accepted_total++;
655 if (!(warm.ex.success && warm_goal)) R.false_applicable++;
656 R.reuse_success[shape] += warm.ex.success && warm_goal;
657 add_series(&R.reuse[shape], &warm);
658 if (i == 1) {
659 CHECK(warm.status_before == PL_STATUS_CANDIDATE && warm.ex.reference_outcome == AG_RUN_SUCCESS,
660 "CANDIDATE reuse runs the reference first");
661 CHECK(warm.t->status == PL_STATUS_VERIFIED, "verified reuse promotes");
662 R.candidate_reference_runs++;
663 R.promotions += warm.t->status == PL_STATUS_VERIFIED;
664 } else {
665 CHECK(warm.status_before == PL_STATUS_VERIFIED && warm.ex.reference_outcome == 0,
666 "VERIFIED reuse skips the reference");
667 }
668 memcpy(real[n_real++], warm.ex.realization_id, 32);
669 CHECK(memcmp(warm.semantic_id, first_id, 32) == 0, "reuse %u same semantic id", i);
670 }
671 }
672 /* Outcome and evidence are the first-use outcome and evidence. */
673 int same = cold.ex.success == warm.ex.success && cold.moves == warm.moves && cold_goal == warm_goal;
674 int ev = cold.ex.evidence_present == warm.ex.evidence_present &&
675 cold.ex.evidence_required == warm.ex.evidence_required &&
676 warm.ex.evidence_present == warm.ex.evidence_required;
677 CHECK(same, "outcome %u: moves %u vs %u", i, cold.moves, warm.moves);
678 CHECK(ev, "evidence %u: %u/%u vs %u/%u", i, cold.ex.evidence_present, cold.ex.evidence_required,
679 warm.ex.evidence_present, warm.ex.evidence_required);
680 CHECK(warm.ex.crumbs_checked > 0 && !(warm.ex.fail & PL_FAIL_CRUMBS), "crumbs verify %u", i);
681 R.outcome_equal[shape] += same;
682 R.evidence_equal[shape] += ev;
683 R.moves[shape] = cold.moves;
684 R.evidence_words[shape] = warm.ex.evidence_present;
685 R.nodes[shape] = warm.ex.nodes;
686 env_stop(g_b);
687 }
688 R.realizations_distinct[shape] = 1;
689 for (uint32_t a = 0; a < n_real; a++)
690 for (uint32_t b = a + 1; b < n_real; b++)
691 if (memcmp(real[a], real[b], 32) == 0) R.realizations_distinct[shape] = 0;
692 CHECK(R.semantic_ids_equal[shape], "one semantic id across bindings");
693 CHECK(R.realizations_distinct[shape], "distinct realizations");
694 Series *f = &R.first[shape], *u = &R.reuse[shape];
695 if (!R.perf_ok) printf(" (instruction counter unavailable: perf_event_paranoid restricts it)\n");
696 printf(" first use: median %llu cognitive ops, %.1f us cpu, %llu instructions; e2e %.1f us\n",
697 (unsigned long long)median(f->ops, f->n), median(f->cpu, f->n) / 1e3,
698 (unsigned long long)median(f->ins, f->n), median(f->e2e, f->n) / 1e3);
699 printf(" reuse: median %llu cognitive ops, %.1f us cpu, %llu instructions; e2e %.1f us\n",
700 (unsigned long long)median(u->ops, u->n), median(u->cpu, u->n) / 1e3,
701 (unsigned long long)median(u->ins, u->n), median(u->e2e, u->n) / 1e3);
702 printf(" plan %u moves, %u graph nodes after optimisation, %u evidence words; accepted %u, succeeded %u\n",
703 R.moves[shape], R.nodes[shape], R.evidence_words[shape], R.reuse_accepted[shape],
704 R.reuse_success[shape]);
705}
706
707static PlAxis expected_axis(Pert p) {
708 switch (p) {
709 case PERT_BINDING: return PL_AX_BINDING;
710 case PERT_GOAL: return PL_AX_GOAL;
711 case PERT_WORLD_GEN: return PL_AX_WORLD_GEN;
712 case PERT_COG_GEN: return PL_AX_COG_GEN;
713 case PERT_ENV: return PL_AX_ENVIRONMENT;
714 case PERT_STATE_SWAP: case PERT_STATE_OUTSIDER: return PL_AX_STATE;
715 case PERT_AUTHORITY: return PL_AX_AUTHORITY;
716 case PERT_RESOURCES: return PL_AX_RESOURCES;
717 default: return PL_AX_SHAPE;
718 }
719}
720
721static void adversarial_case(uint32_t shape, uint32_t seed, Pert p, const PlanTemplate *stored) {
722 Spec sp = spec(shape, seed, p);
723 if (p == PERT_WORLD_GEN) sp.world_gen = WORLD_GEN + 1;
724 if (p == PERT_COG_GEN) sp.cog_gen = COG_GEN + 1;
725 if (p == PERT_GOAL) sp.max_steps = stored ? stored->n_steps - 1 : 1;
726 PlAxis want = expected_axis(p);
727 Trial t;
728 CHECK(env_build(g_b, &sp) == 0, "build adversarial");
729 solve(g_b, &g_cache, MODE_DECIDE_ONLY, &t);
730 R.adv_cases++;
731 R.adv_by_axis[want]++;
732 if (t.path == PATH_REUSE) {
733 /* Accepted: it ran. Counted against false applicability if it did not verify. */
734 R.accepted_total++;
735 if (!(t.ex.success && goal_holds(g_b))) R.false_applicable++;
736 CHECK(0, "adversarial %s seed %u was accepted", rx_plan_axis_name(want), seed);
737 } else {
738 R.adv_refused++;
739 R.adv_refused_by_axis[want]++;
740 /* The template made for this start arrangement must be refused on the
741 * perturbed axis. Other templates of the same goal shape (a different
742 * start) may be refused on an earlier or later axis of their own. */
743 PlAxis got = PL_AX_SHAPE;
744 int pr = -1, found = !stored;
745 for (uint32_t i = 0; i < t.rf.n; i++)
746 if (t.rf.t[i] == stored) { got = t.rf.ax[i]; pr = t.rf.pred[i]; found = 1; }
747 R.adv_axis_match += found && got == want;
748 CHECK(found && got == want, "adversarial seed %u: its template refused on %s (pred %d), expected %s",
749 seed, rx_plan_axis_name(got), pr, rx_plan_axis_name(want));
750 /* Every refusal is a World decision with its own crumb. */
751 int crumb_ok = 1;
752 uint32_t n_rec = t.rf.n ? t.rf.n : 1;
753 for (uint32_t i = 0; i < n_rec; i++) {
754 int64_t id = t.rf.n ? t.rf.crumb[i] : t.decision_crumb;
755 const RxCrumb *k = id > 0 ? rx_world_crumb(&g_b->w, (uint64_t)id) : NULL;
756 crumb_ok &= k && k->kind == RX_CRUMB_EXTERNAL && k->n_outputs == 1 &&
757 k->outputs[0].obj.id == g_b->decision.id;
758 }
759 RxObject d;
760 crumb_ok = crumb_ok && rx_world_read(&g_b->w, g_b->decision, &d) == RX_OK &&
761 d.field[1] == (uint64_t)(t.rf.n ? t.rf.ax[t.rf.n - 1] : PL_AX_SHAPE);
762 R.adv_decision_crumbs += crumb_ok;
763 CHECK(crumb_ok, "every refusal recorded in the World");
764 /* Blind replay: bind and run it anyway. */
765 if (stored) {
766 PlaGoal g;
767 RxObjRef s[PL_MAX_SLOTS];
768 rx_plan_view(&g_b->w, &g_view);
769 if (pla_goal_read(&g_view, &g_b->env, &g, NULL) == 0 && pla_bind(stored, &g_view, &g, 0, s, NULL) > 0) {
770 PlExecResult ex;
771 g_b->x.reference_first = 0;
772 rx_plan_execute(stored, &g_b->w, &g_b->env, s, &g_b->caps, &g_b->x, 1, &g_b->rs, pla_legal, &ex);
773 int ok = ex.success && goal_holds(g_b);
774 R.blind_by_axis[want]++;
775 R.blind_ok_by_axis[want] += ok;
776 /* A violation: it did not reach the goal, or it broke a stated condition. */
777 int violated = !ok || p == PERT_GOAL || p == PERT_ENV || p == PERT_WORLD_GEN ||
778 p == PERT_COG_GEN;
779 R.blind_violation_by_axis[want] += violated;
780 }
781 }
782 }
783 env_stop(g_b);
784}
785
786static void t_adversarial(void) {
787 printf("[*] adversarial goals of stored shapes: every applicability axis\n");
788 PlanTemplate *rev = NULL, *mer = NULL;
789 for (uint32_t i = 0; i < g_cache.n; i++) {
790 if (g_cache.t[i]->ancestry.origin != PL_ORIGIN_SEARCH) continue;
791 if (g_cache.t[i]->n_goal_slots == 6 && !rev && g_cache.t[i]->graph.n_nodes && g_cache.t[i]->success.n == 6) {
792 /* distinguish by shape: the first stored is reverse */
793 rev = g_cache.t[i];
794 } else if (!mer) {
795 mer = g_cache.t[i];
796 }
797 }
798 const Pert perts[] = { PERT_BINDING, PERT_GOAL, PERT_WORLD_GEN, PERT_COG_GEN, PERT_ENV,
799 PERT_STATE_SWAP, PERT_STATE_OUTSIDER, PERT_AUTHORITY, PERT_RESOURCES };
800 for (uint32_t k = 0; k < sizeof perts / sizeof perts[0]; k++)
801 for (uint32_t i = 0; i < N_ADV; i++) {
802 uint32_t shape = i % 2 ? SHAPE_MERGE : SHAPE_REVERSE;
803 adversarial_case(shape, 5000 + 100 * k + i, perts[k], shape == SHAPE_MERGE ? mer : rev);
804 }
805 for (uint32_t i = 0; i < N_ADV; i++) adversarial_case(SHAPE_OTHER, 7000 + i, PERT_NONE, NULL);
806 printf(" %u adversarial goals, %u refused (%u on the expected axis), %u refusals recorded as World decisions\n",
807 R.adv_cases, R.adv_refused, R.adv_axis_match, R.adv_decision_crumbs);
808 for (uint32_t a = 1; a < PL_AX_COUNT; a++)
809 if (R.adv_by_axis[a])
810 printf(" %-22s %2u refused of %2u; blind replay reached the goal %u of %u, violated %u\n",
811 rx_plan_axis_name((PlAxis)a), R.adv_refused_by_axis[a], R.adv_by_axis[a],
812 R.blind_ok_by_axis[a], R.blind_by_axis[a], R.blind_violation_by_axis[a]);
813}
814
815static void t_rederive(void) {
816 printf("[*] new World generation: refuse, re-derive, same semantic id, reuse again\n");
817 uint32_t n_before = g_cache.n;
818 Spec sp = spec(SHAPE_REVERSE, 8000, PERT_NONE);
819 sp.world_gen = WORLD_GEN + 1;
820 Trial t;
821 CHECK(env_build(g_b, &sp) == 0, "build");
822 solve(g_b, &g_cache, MODE_WARM, &t);
823 CHECK(t.path == PATH_FIRST && t.ex.success, "refused then planned (path %d)", t.path);
824 PlanTemplate *have = t.t;
825 CHECK(g_cache.n == n_before && have && have->rederivations == 1 &&
826 have->verified_world_gen == WORLD_GEN + 1, "re-derived into the existing record");
827 R.rederived_same_id = g_cache.n == n_before && have && have->rederivations == 1;
828 env_stop(g_b);
829 sp.seed = 8001;
830 CHECK(env_build(g_b, &sp) == 0, "build");
831 solve(g_b, &g_cache, MODE_WARM, &t);
832 CHECK(t.path == PATH_REUSE && t.ex.success && t.ex.reference_outcome == AG_RUN_SUCCESS,
833 "reused under the new generation after re-derivation");
834 R.rederived_then_reused = t.path == PATH_REUSE && t.ex.success;
835 if (t.path == PATH_REUSE) {
836 R.accepted_total++;
837 if (!t.ex.success) R.false_applicable++;
838 }
839 env_stop(g_b);
840}
841
842static void t_adapt(void) {
843 printf("[*] adaptation: an outsider sits on the tower\n");
844 uint64_t ops[N_ADV], cold_ops[N_ADV], cpu[N_ADV], cold_cpu[N_ADV];
845 uint32_t na = 0;
846 for (uint32_t i = 0; i < N_ADV; i++) {
847 Spec sp = spec(SHAPE_REVERSE, 9000 + i, PERT_STATE_OUTSIDER);
848 sp.world_gen = WORLD_GEN + 1; /* the generation the organism is at after t_rederive */
849 Trial cold, warm;
850 CHECK(env_build(g_a, &sp) == 0, "build");
851 solve(g_a, NULL, MODE_COLD, &cold);
852 env_stop(g_a);
853 CHECK(env_build(g_b, &sp) == 0, "build");
854 solve(g_b, &g_cache, MODE_WARM_ADAPT, &warm);
855 int ok = warm.ex.success && goal_holds(g_b);
856 CHECK(ok, "adapt %u path %d", i, warm.path);
857 if (getenv("PLAN_REUSE_DEBUG")) fprintf(stderr, " adapt %u path %d prefix %u total %u cold %u\n", i, warm.path, warm.prefix_moves, warm.moves, cold.moves);
858 if (i == 0) {
859 CHECK(warm.path == PATH_ADAPT, "first outsider goal adapts (path %d)", warm.path);
860 R.adapt_n++;
861 R.adapt_ok += ok && warm.path == PATH_ADAPT;
862 ops[na] = pl_cost_ops(&warm.cost);
863 cpu[na] = warm.cog_cpu;
864 cold_ops[na] = pl_cost_ops(&cold.cost);
865 cold_cpu[na++] = cold.cog_cpu;
866 R.adapt_steps = warm.moves;
867 R.adapt_cold_steps = cold.moves;
868 CHECK(warm.t && warm.t->ancestry.origin == PL_ORIGIN_ADAPTED && warm.t->ancestry.has_parent,
869 "adapted template records its parent");
870 } else {
871 R.adapt_then_reuse += warm.path == PATH_REUSE && ok;
872 if (warm.path == PATH_REUSE) {
873 R.accepted_total++;
874 if (!ok) R.false_applicable++;
875 }
876 }
877 env_stop(g_b);
878 }
879 R.adapt_ops_med = median(ops, na);
880 R.adapt_cold_ops_med = median(cold_ops, na);
881 R.adapt_cpu_med = median(cpu, na);
882 R.adapt_cold_cpu_med = median(cold_cpu, na);
883 printf(" adapted: %llu ops, %.1f us cpu, %u steps; full search: %llu ops, %.1f us cpu, %u steps; "
884 "then reused %u of %u\n", (unsigned long long)R.adapt_ops_med, R.adapt_cpu_med / 1e3,
885 R.adapt_steps, (unsigned long long)R.adapt_cold_ops_med, R.adapt_cold_cpu_med / 1e3,
886 R.adapt_cold_steps, R.adapt_then_reuse, N_ADV - 1);
887}
888
889static void t_identity(void) {
890 printf("[*] identity\n");
891 PlanTemplate *t = g_cache.t[0];
892 static uint8_t a[1u << 16], b[1u << 16];
893 size_t na = rx_plan_encode(t, a, sizeof a);
894 CHECK(na > 0 && rx_plan_decode(a, na, &g_tmp) == 0, "decode");
895 size_t nb = rx_plan_encode(&g_tmp, b, sizeof b);
896 R.id_roundtrip = na == nb && memcmp(a, b, na) == 0;
897 CHECK(R.id_roundtrip, "canonical round trip");
898 /* Builder order. */
899 g_tmp = *t;
900 PlPredList *lists[] = { &g_tmp.pre, &g_tmp.state, &g_tmp.env, &g_tmp.invariants, &g_tmp.success };
901 for (uint32_t l = 0; l < 5; l++)
902 for (uint32_t i = 0; i < lists[l]->n / 2; i++) {
903 PlPred x = lists[l]->p[i];
904 lists[l]->p[i] = lists[l]->p[lists[l]->n - 1 - i];
905 lists[l]->p[lists[l]->n - 1 - i] = x;
906 }
907 rx_plan_identify(&g_tmp);
908 R.id_order_invariant = memcmp(g_tmp.semantic_id, t->semantic_id, 32) == 0;
909 CHECK(R.id_order_invariant, "predicate order does not change the id");
910 /* Every semantic field changes it. */
911 for (int m = 0; m < 10; m++) {
912 g_tmp = *t;
913 switch (m) {
914 case 0: g_tmp.goal_kind++; break;
915 case 1: g_tmp.goal_shape[3] ^= 1; break;
916 case 2: g_tmp.slots[0].rights ^= RX_RIGHT_WRITE; break;
917 case 3: g_tmp.state.p[0].value ^= 1; break;
918 case 4: g_tmp.env.p[0].value ^= 1; break;
919 case 5: g_tmp.success.p[0].slot2 ^= 1; break;
920 case 6: g_tmp.n_steps++; break;
921 case 7: g_tmp.step_need.energy_cost++; break;
922 case 8: g_tmp.invariants.n--; break;
923 case 9: g_tmp.graph.nodes[0].field ^= 1; break;
924 }
925 rx_plan_identify(&g_tmp);
926 R.id_mutations++;
927 R.id_mutations_changed += memcmp(g_tmp.semantic_id, t->semantic_id, 32) != 0;
928 }
929 CHECK(R.id_mutations_changed == R.id_mutations, "semantic changes change the id");
930 /* Non-semantic fields do not. */
931 for (int m = 0; m < 4; m++) {
932 g_tmp = *t;
933 switch (m) {
934 case 0: g_tmp.status = PL_STATUS_STALE; break;
935 case 1: g_tmp.ancestry.goal_seq += 99; g_tmp.ancestry.origin = PL_ORIGIN_ADAPTED; break;
936 case 2: g_tmp.uses += 7; g_tmp.refusals[3] += 2; break;
937 case 3: g_tmp.verified_world_gen += 5; break;
938 }
939 rx_plan_identify(&g_tmp);
940 R.id_nonsemantic++;
941 R.id_nonsemantic_same += memcmp(g_tmp.semantic_id, t->semantic_id, 32) == 0;
942 }
943 CHECK(R.id_nonsemantic_same == R.id_nonsemantic, "ancestry and status are not semantics");
944}
945
946/* ---- energy (reported, not gated) ---- */
947
948static int hwmon_dir(char *out, size_t n) {
949 for (int i = 0; i < 16; i++) {
950 char p[96], name[64] = { 0 };
951 snprintf(p, sizeof p, "/sys/class/hwmon/hwmon%d/name", i);
952 FILE *f = fopen(p, "r");
953 if (!f) continue;
954 int ok = fgets(name, sizeof name, f) != NULL;
955 fclose(f);
956 if (ok && strncmp(name, "aien_spbm", 9) == 0) {
957 snprintf(out, n, "/sys/class/hwmon/hwmon%d", i);
958 return 0;
959 }
960 }
961 return -1;
962}
963
964static int energy_uj(const char *dir, int ch, uint64_t *v) {
965 char p[160];
966 snprintf(p, sizeof p, "%s/energy%d_input", dir, ch);
967 FILE *f = fopen(p, "r");
968 if (!f) return -1;
969 unsigned long long x = 0;
970 int ok = fscanf(f, "%llu", &x) == 1;
971 fclose(f);
972 *v = x;
973 return ok ? 0 : -1;
974}
975
976/* An R15 timed measurement is running: its quiet flag exists, or an R15
977 * program is alive (matched on the program name, not on command lines). */
978static int other_load(void) {
979 const char *home = getenv("HOME");
980 char flag[512];
981 snprintf(flag, sizeof flag, "%s/workspace/.spark-quiet", home ? home : "");
982 if (access(flag, F_OK) == 0) return 1;
983 DIR *d = opendir("/proc");
984 if (!d) return 0;
985 struct dirent *de;
986 int busy = 0;
987 while (!busy && (de = readdir(d)) != NULL) {
988 if (de->d_name[0] < '0' || de->d_name[0] > '9') continue;
989 char p[300], comm[64] = { 0 };
990 snprintf(p, sizeof p, "/proc/%s/comm", de->d_name);
991 FILE *f = fopen(p, "r");
992 if (!f) continue;
993 if (fgets(comm, sizeof comm, f))
994 busy = strncmp(comm, "rx_r15", 6) == 0 || strncmp(comm, "r15_", 4) == 0;
995 fclose(f);
996 }
997 closedir(d);
998 return busy;
999}
1000
1001static void t_energy(void) {
1002 printf("[*] energy per cognition (package telemetry; reported, not gated)\n");
1003 char dir[96];
1004 if (hwmon_dir(dir, sizeof dir) != 0) {
1005 snprintf(R.energy_why, sizeof R.energy_why, "no aien_spbm hwmon reader");
1006 printf(" unavailable: %s\n", R.energy_why);
1007 return;
1008 }
1009 if (other_load()) {
1010 snprintf(R.energy_why, sizeof R.energy_why, "an R15 measurement was running; not measured");
1011 printf(" %s\n", R.energy_why);
1012 return;
1013 }
1014 Spec sp = spec(SHAPE_REVERSE, 100, PERT_NONE);
1015 sp.world_gen = WORLD_GEN + 1;
1016 CHECK(env_build(g_b, &sp) == 0, "build");
1017 PlaGoal g;
1018 const uint64_t T = 4000000000ull;
1019 uint64_t p0, p1, c0, c1, t0, t1;
1020 /* Idle baseline. */
1021 energy_uj(dir, 1, &p0); energy_uj(dir, 3, &c0); t0 = now_ns();
1022 struct timespec ts = { 3, 0 };
1023 nanosleep(&ts, NULL);
1024 energy_uj(dir, 1, &p1); energy_uj(dir, 3, &c1); t1 = now_ns();
1025 double idle_pkg_w = (double)(p1 - p0) / 1e6 / ((double)(t1 - t0) / 1e9);
1026 double idle_cpup_w = (double)(c1 - c0) / 1e6 / ((double)(t1 - t0) / 1e9);
1027 R.idle_w = idle_pkg_w;
1028 for (int phase = 0; phase < 2; phase++) {
1029 uint64_t loops = 0;
1030 energy_uj(dir, 1, &p0); energy_uj(dir, 3, &c0); t0 = now_ns();
1031 do {
1032 PlCost cost;
1033 memset(&cost, 0, sizeof cost);
1034 rx_plan_view(&g_b->w, &g_view);
1035 pla_goal_read(&g_view, &g_b->env, &g, &cost);
1036 if (phase == 0) {
1037 PlaTarget tg;
1038 PlaPlan plan;
1039 RxObjRef s[PL_MAX_SLOTS];
1040 pla_target_from_goal(&g, &tg);
1041 pla_plan(&g_view, &g, &tg, MAX_EXPAND, &plan, &cost);
1042 pla_generalize(&g_view, &g, &plan, PL_ORIGIN_SEARCH, NULL, COG_GEN, &g_tmp2, s);
1043 } else {
1044 RxObjRef s[PL_MAX_SLOTS];
1045 static Refusals rf;
1046 if (!decide(g_b, &g_cache, &g_view, &g, s, &cost, &rf)) R.energy_reuse_refused++;
1047 }
1048 loops++;
1049 } while (now_ns() - t0 < T);
1050 energy_uj(dir, 1, &p1); energy_uj(dir, 3, &c1); t1 = now_ns();
1051 double secs = (double)(t1 - t0) / 1e9;
1052 double pkg_mj = (double)(p1 - p0) / 1e3 - idle_pkg_w * secs * 1e3;
1053 double cpup_mj = (double)(c1 - c0) / 1e3 - idle_cpup_w * secs * 1e3;
1054 if (phase == 0) {
1055 R.cold_loops = loops;
1056 R.cold_pkg_mj_per = pkg_mj / (double)loops;
1057 R.cold_cpup_mj_per = cpup_mj / (double)loops;
1058 } else {
1059 R.reuse_loops = loops;
1060 R.reuse_pkg_mj_per = pkg_mj / (double)loops;
1061 R.reuse_cpup_mj_per = cpup_mj / (double)loops;
1062 }
1063 }
1064 CHECK(R.energy_reuse_refused == 0, "energy reuse loop measured accepted reuses (%llu refused)",
1065 (unsigned long long)R.energy_reuse_refused);
1066 R.loop_ns = T;
1067 R.energy_ok = 1;
1068 env_stop(g_b);
1069 printf(" idle package %.2f W; first-use cognition %.4f mJ (package) / %.4f mJ (P cores) over %llu loops; "
1070 "reuse cognition %.6f mJ / %.6f mJ over %llu loops\n", idle_pkg_w, R.cold_pkg_mj_per,
1071 R.cold_cpup_mj_per, (unsigned long long)R.cold_loops, R.reuse_pkg_mj_per, R.reuse_cpup_mj_per,
1072 (unsigned long long)R.reuse_loops);
1073}
1074
1075/* ---- placement and receipt ---- */
1076
1077static void place(void) {
1078 cpu_set_t set;
1079 CPU_ZERO(&set);
1080 int count = 0;
1081 char *p = R.cpus;
1082 long n = sysconf(_SC_NPROCESSORS_CONF);
1083 for (long c = 0; c < n; c++) {
1084 char path[128];
1085 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c);
1086 FILE *fp = fopen(path, "r");
1087 if (!fp) continue;
1088 unsigned long long midr = 0;
1089 int ok = fscanf(fp, "%llx", &midr) == 1;
1090 fclose(fp);
1091 if (ok && ((midr >> 4) & 0xfffu) == 0xd85u) {
1092 CPU_SET(c, &set);
1093 count++;
1094 R.core_class = 0xd85u;
1095 if (p < R.cpus + sizeof R.cpus - 5) p += sprintf(p, "%s%ld", count > 1 ? "," : "", c);
1096 }
1097 }
1098 if (count == 0 || sched_setaffinity(0, sizeof set, &set) != 0) {
1099 snprintf(R.cpus, sizeof R.cpus, "unpinned");
1100 return;
1101 }
1102 printf("[*] placed on %d Cortex-X925 cores: %s\n", count, R.cpus);
1103}
1104
1105static void binary_digest(char out[65]) {
1106 strcpy(out, "unavailable");
1107 FILE *fp = fopen("/proc/self/exe", "rb");
1108 if (!fp) return;
1109 sha256_ctx c;
1110 sha256_init(&c);
1111 uint8_t buf[65536];
1112 size_t n;
1113 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n);
1114 fclose(fp);
1115 uint8_t d[32];
1116 sha256_final(&c, d);
1117 for (int i = 0; i < 32; i++) sprintf(out + i * 2, "%02x", d[i]);
1118}
1119
1120typedef struct {
1121 int pass, task_gate;
1122 int w_g1[2], w_g2[2], w_g3[2], w_g6[2];
1123 double ops_ratio[2], cpu_ratio[2], e2e_ratio[2], cpu_ratio_no_ident[2];
1124 uint64_t ident_med[2];
1125} Gates;
1126
1127static int g1, g2, g3, g4, g5, g6;
1128
1129static Gates gates(void) {
1130 Gates G;
1131 memset(&G, 0, sizeof G);
1132 g1 = g2 = g6 = 1;
1133 for (int s = 0; s < 2; s++) {
1134 Series *f = &R.first[s], *u = &R.reuse[s];
1135 uint64_t fo = median(f->ops, f->n), uo = median(u->ops, u->n);
1136 uint64_t fc = median(f->cpu, f->n), uc = median(u->cpu, u->n);
1137 uint64_t fe = median(f->e2e, f->n), ue = median(u->e2e, u->n);
1138 G.ops_ratio[s] = uo ? (double)fo / (double)uo : 0;
1139 G.cpu_ratio[s] = uc ? (double)fc / (double)uc : 0;
1140 G.e2e_ratio[s] = ue ? (double)fe / (double)ue : 0;
1141 uint64_t d[N_REPEAT];
1142 for (uint32_t i = 0; i < f->n; i++) d[i] = f->cpu[i] > f->ident[i] ? f->cpu[i] - f->ident[i] : 0;
1143 G.ident_med[s] = median(f->ident, f->n);
1144 G.cpu_ratio_no_ident[s] = uc ? (double)median(d, f->n) / (double)uc : 0;
1145 G.w_g1[s] = u->n > 0 && uo * 20 <= fo;
1146 G.w_g2[s] = u->n > 0 && uc * 10 <= fc;
1147 G.w_g6[s] = u->n > 0 && ue < fe;
1148 g1 &= G.w_g1[s];
1149 g2 &= G.w_g2[s];
1150 g6 &= G.w_g6[s];
1151 }
1152 g3 = 1;
1153 for (int s = 0; s < 2; s++)
1154 {
1155 G.w_g3[s] = R.reuse_accepted[s] == N_REPEAT - 1 && R.reuse_success[s] == R.reuse_accepted[s] &&
1156 R.outcome_equal[s] == N_REPEAT && R.evidence_equal[s] == N_REPEAT;
1157 g3 &= G.w_g3[s];
1158 }
1159 uint32_t per_axis_ok = 1;
1160 for (uint32_t a = PL_AX_SHAPE; a <= PL_AX_RESOURCES; a++)
1161 per_axis_ok &= R.adv_by_axis[a] >= 8 && R.adv_refused_by_axis[a] == R.adv_by_axis[a];
1162 g4 = R.false_applicable == 0 && R.adv_cases >= 80 && R.adv_refused == R.adv_cases &&
1163 R.adv_decision_crumbs == R.adv_refused && per_axis_ok;
1164 g5 = R.id_roundtrip && R.id_order_invariant && R.id_mutations_changed == R.id_mutations &&
1165 R.id_nonsemantic_same == R.id_nonsemantic && R.semantic_ids_equal[0] && R.semantic_ids_equal[1] &&
1166 R.realizations_distinct[0] && R.realizations_distinct[1] && R.rederived_same_id;
1167 G.pass = g1 && g2 && g3 && g4 && g5 && g6 && g_fail == 0;
1168 /* The task's own wording: at least one repeated workload. Reported beside
1169 * the pre-registered verdict, never in its place. */
1170 for (int s = 0; s < 2; s++)
1171 G.task_gate |= G.w_g1[s] && G.w_g2[s] && G.w_g3[s] && G.w_g6[s];
1172 G.task_gate = G.task_gate && g4 && g5 && g_fail == 0;
1173 return G;
1174}
1175
1176static void json_series(FILE *fp, const char *key, const Series *s) {
1177 fprintf(fp, " \"%s\": {\"n\": %u, \"median_cognitive_ops\": %llu, \"median_cognition_cpu_ns\": %llu, "
1178 "\"median_cognition_wall_ns\": %llu, \"median_instructions\": %llu, \"median_execution_ns\": %llu, "
1179 "\"median_end_to_end_ns\": %llu, \"median_compile_ns\": %llu, \"median_graph_run_ns\": %llu, \"median_verify_ns\": %llu}",
1180 key, s->n, (unsigned long long)median(s->ops, s->n), (unsigned long long)median(s->cpu, s->n),
1181 (unsigned long long)median(s->wall, s->n), (unsigned long long)median(s->ins, s->n),
1182 (unsigned long long)median(s->exec, s->n), (unsigned long long)median(s->e2e, s->n),
1183 (unsigned long long)median(s->compile, s->n), (unsigned long long)median(s->run, s->n),
1184 (unsigned long long)median(s->verify, s->n));
1185}
1186
1187static void write_receipt(void) {
1188 Gates G = gates();
1189 char path[512];
1190 /* Built in memory, then stored under its content address, so a later
1191 * run can never overwrite this receipt. */
1192 char *rbuf = NULL;
1193 size_t rlen = 0;
1194 FILE *fp = open_memstream(&rbuf, &rlen);
1195 if (!fp) return;
1196 char commit[41];
1197 memset(commit, 0, sizeof commit);
1198 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8);
1199 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT");
1200 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && !omega_evidence_tree_dirty();
1201 const char *aienos = getenv("AIENOS_COMMIT");
1202 char digest[65];
1203 binary_digest(digest);
1204 struct utsname u;
1205 memset(&u, 0, sizeof u);
1206 uname(&u);
1207 fprintf(fp, "{\n \"schema\": \"OMEGA_PLAN_REUSE_V1\",\n \"run_id\": \"%s\",\n", omega_evidence_run_id());
1208 fprintf(fp, " \"candidate_commit\": %s%s%s,\n \"candidate_bound\": %s,\n \"run_commit\": \"%s\",\n"
1209 " \"tree_dirty\": %s,\n \"aienos_commit\": %s%s%s,\n",
1210 candidate ? "\"" : "", candidate ? candidate : "null", candidate ? "\"" : "", bound ? "true" : "false",
1211 commit, omega_evidence_tree_dirty() ? "true" : "false", aienos ? "\"" : "", aienos ? aienos : "null",
1212 aienos ? "\"" : "");
1213 fprintf(fp, " \"checks\": %d,\n \"failures\": %d,\n \"test_binary_sha256\": \"%s\",\n", g_checks, g_fail, digest);
1214 fprintf(fp, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", \"cpus\": \"%s\", "
1215 "\"core_class_midr_part\": \"0x%llx\"},\n",
1216 u.sysname, u.release, u.machine, R.cpus, (unsigned long long)R.core_class);
1217 fprintf(fp, " \"scope\": \"host processor; native AIENOS authority library; resident reaction world; "
1218 "one planning domain (arrangement); planner is explicit A* search, not neural\",\n");
1219 fprintf(fp, " \"cognitive_ops_definition\": \"first use: states expanded + successors generated + heuristic "
1220 "evaluations + goal-shape steps; reuse: goal-shape steps + candidates retrieved + predicates "
1221 "evaluated + binding attempts\",\n");
1222 fprintf(fp, " \"workloads\": {\n");
1223 const char *names[2] = { "reverse_tower_6", "merge_two_towers_3_3" };
1224 for (int s = 0; s < 2; s++) {
1225 fprintf(fp, " \"%s\": {\n \"instances\": %u, \"plan_steps\": %u, \"graph_nodes\": %u, "
1226 "\"evidence_words\": %u,\n",
1227 names[s], N_REPEAT, R.moves[s], R.nodes[s], R.evidence_words[s]);
1228 json_series(fp, "first_use", &R.first[s]);
1229 fprintf(fp, ",\n");
1230 json_series(fp, "reuse", &R.reuse[s]);
1231 fprintf(fp, ",\n \"ratios\": {\"cognitive_ops\": %.1f, \"cognition_cpu\": %.1f, "
1232 "\"cognition_cpu_excluding_identity_hash\": %.1f, \"end_to_end\": %.2f},\n"
1233 " \"first_use_median_identity_hash_cpu_ns\": %llu,\n"
1234 " \"criteria\": {\"G1\": \"%s\", \"G2\": \"%s\", \"G3\": \"%s\", \"G6\": \"%s\"},\n"
1235 " \"reuse_accepted\": %u, \"reuse_succeeded\": %u, \"outcome_equal_to_first_use\": %u, "
1236 "\"evidence_equal_to_first_use\": %u, \"one_semantic_id\": %s, \"realizations_distinct\": %s\n }%s\n",
1237 G.ops_ratio[s], G.cpu_ratio[s], G.cpu_ratio_no_ident[s], G.e2e_ratio[s],
1238 (unsigned long long)G.ident_med[s], G.w_g1[s] ? "PASS" : "FAIL", G.w_g2[s] ? "PASS" : "FAIL",
1239 G.w_g3[s] ? "PASS" : "FAIL", G.w_g6[s] ? "PASS" : "FAIL", R.reuse_accepted[s], R.reuse_success[s],
1240 R.outcome_equal[s], R.evidence_equal[s], R.semantic_ids_equal[s] ? "true" : "false",
1241 R.realizations_distinct[s] ? "true" : "false", s == 0 ? "," : "");
1242 }
1243 fprintf(fp, " },\n \"lifecycle\": {\"candidate_reference_runs\": %u, \"promotions_to_verified\": %u, "
1244 "\"templates_stored\": %llu, \"rederivations_deduplicated\": %llu},\n",
1245 R.candidate_reference_runs, R.promotions, (unsigned long long)g_cache.stored,
1246 (unsigned long long)g_cache.deduplicated);
1247 fprintf(fp, " \"applicability\": {\"accepted_reuses\": %u, \"false_applicable\": %u, \"false_applicability_rate\": %.4f,\n"
1248 " \"adversarial\": %u, \"refused\": %u, \"refused_on_expected_axis\": %u, \"refusals_recorded_in_world\": %u,\n"
1249 " \"by_axis\": {",
1250 R.accepted_total, R.false_applicable,
1251 R.accepted_total ? (double)R.false_applicable / (double)R.accepted_total : 0.0, R.adv_cases,
1252 R.adv_refused, R.adv_axis_match, R.adv_decision_crumbs);
1253 int first = 1;
1254 for (uint32_t a = 1; a < PL_AX_COUNT; a++) {
1255 if (!R.adv_by_axis[a]) continue;
1256 fprintf(fp, "%s\n \"%s\": {\"cases\": %u, \"refused\": %u, \"blind_replays\": %u, "
1257 "\"blind_reached_goal\": %u, \"blind_violated\": %u}",
1258 first ? "" : ",", rx_plan_axis_name((PlAxis)a), R.adv_by_axis[a], R.adv_refused_by_axis[a],
1259 R.blind_by_axis[a], R.blind_ok_by_axis[a], R.blind_violation_by_axis[a]);
1260 first = 0;
1261 }
1262 fprintf(fp, "\n }},\n");
1263 fprintf(fp, " \"identity\": {\"canonical_round_trip\": %s, \"predicate_order_invariant\": %s, "
1264 "\"semantic_mutations\": %u, \"changed_id\": %u, \"nonsemantic_mutations\": %u, \"kept_id\": %u, "
1265 "\"rederived_same_id\": %s, \"rederived_then_reused\": %s},\n",
1266 R.id_roundtrip ? "true" : "false", R.id_order_invariant ? "true" : "false", R.id_mutations,
1267 R.id_mutations_changed, R.id_nonsemantic, R.id_nonsemantic_same, R.rederived_same_id ? "true" : "false",
1268 R.rederived_then_reused ? "true" : "false");
1269 fprintf(fp, " \"adaptation\": {\"adapted\": %u, \"succeeded\": %u, \"cognitive_ops\": %llu, "
1270 "\"full_search_cognitive_ops\": %llu, \"cognition_cpu_ns\": %llu, \"full_search_cognition_cpu_ns\": %llu, "
1271 "\"steps\": %u, \"full_search_steps\": %u, \"later_goals_reused_adapted\": %u},\n",
1272 R.adapt_n, R.adapt_ok, (unsigned long long)R.adapt_ops_med, (unsigned long long)R.adapt_cold_ops_med,
1273 (unsigned long long)R.adapt_cpu_med, (unsigned long long)R.adapt_cold_cpu_med, R.adapt_steps,
1274 R.adapt_cold_steps, R.adapt_then_reuse);
1275 if (R.energy_ok)
1276 fprintf(fp, " \"energy\": {\"source\": \"aien_spbm hwmon (on-device package counter, 1 mJ steps; "
1277 "machine not quiet; idle baseline subtracted)\", \"idle_package_w\": %.3f, \"loop_ns\": %llu,\n"
1278 " \"first_use_loops\": %llu, \"first_use_package_mj\": %.5f, \"first_use_cpu_p_mj\": %.5f,\n"
1279 " \"reuse_loops\": %llu, \"reuse_package_mj\": %.7f, \"reuse_cpu_p_mj\": %.7f},\n",
1280 R.idle_w, (unsigned long long)R.loop_ns, (unsigned long long)R.cold_loops, R.cold_pkg_mj_per,
1281 R.cold_cpup_mj_per, (unsigned long long)R.reuse_loops, R.reuse_pkg_mj_per, R.reuse_cpup_mj_per);
1282 else
1283 fprintf(fp, " \"energy\": {\"measured\": false, \"why\": \"%s\"},\n", R.energy_why);
1284 fprintf(fp, " \"instructions_counter\": %s,\n", R.perf_ok ? "true" : "false");
1285 fprintf(fp, " \"gates\": {\n \"G1_cognitive_ops_20x\": \"%s\",\n \"G2_cognition_cpu_10x\": \"%s\",\n"
1286 " \"G3_outcome_and_evidence\": \"%s\",\n \"G4_false_applicability_zero\": \"%s\",\n"
1287 " \"G5_identity\": \"%s\",\n \"G6_end_to_end_faster\": \"%s\",\n"
1288 " \"OMEGA_PLAN_REUSE_PASS\": \"%s\",\n"
1289 " \"verdict_basis\": \"spec/plan-reuse.md section 7 as pre-registered: G1, G2, G3 and G6 hold for every workload\",\n"
1290 " \"task_wording\": {\"text\": \"at least one repeated workload where verified plan reuse substantially "
1291 "reduces cognitive computation while preserving outcome/evidence\", "
1292 "\"met\": %s, \"workloads_meeting_G1_G2_G3_G6\": [%s%s%s], "
1293 "\"note\": \"reported beside the pre-registered verdict, not in its place\"},\n",
1294 g1 ? "PASS" : "FAIL", g2 ? "PASS" : "FAIL", g3 ? "PASS" : "FAIL", g4 ? "PASS" : "FAIL",
1295 g5 ? "PASS" : "FAIL", g6 ? "PASS" : "FAIL", G.pass ? "PASS" : "FAIL",
1296 G.task_gate ? "true" : "false",
1297 G.w_g1[0] && G.w_g2[0] && G.w_g3[0] && G.w_g6[0] ? "\"reverse_tower_6\"" : "",
1298 G.w_g1[0] && G.w_g2[0] && G.w_g3[0] && G.w_g6[0] && G.w_g1[1] && G.w_g2[1] && G.w_g3[1] && G.w_g6[1] ? ", " : "",
1299 G.w_g1[1] && G.w_g2[1] && G.w_g3[1] && G.w_g6[1] ? "\"merge_two_towers_3_3\"" : "");
1300 fprintf(fp, " \"not_claimed\": [\"a neural or learned planner\", \"more than one planning domain\", "
1301 "\"durable template storage (R9)\", \"graphics-processor execution\", "
1302 "\"cognition reading the World through capabilities (the cache reads a host projection; "
1303 "execution is capability-checked)\", \"templates above 8 slots or 64 graph nodes\"]\n }\n}\n");
1304 int wrc = fclose(fp) == 0 ? omega_evidence_write_digest("PLAN_REUSE/rx_plan_reuse_receipt",
1305 "json", rbuf, rlen, path, sizeof path)
1306 : -1;
1307 free(rbuf);
1308 if (wrc < 0) {
1309 printf("receipt: not written\n");
1310 return;
1311 }
1312 printf("receipt: %s%s\n", path, wrc == 1 ? " (identical receipt already recorded)" : "");
1313 printf("OMEGA_PLAN_REUSE_PASS: %s (G1 %s, G2 %s, G3 %s, G4 %s, G5 %s, G6 %s)\n", G.pass ? "PASS" : "FAIL",
1314 g1 ? "PASS" : "FAIL", g2 ? "PASS" : "FAIL", g3 ? "PASS" : "FAIL", g4 ? "PASS" : "FAIL",
1315 g5 ? "PASS" : "FAIL", g6 ? "PASS" : "FAIL");
1316 for (int s = 0; s < 2; s++)
1317 printf(" %s: ops %.1fx (G1 %s), cpu %.1fx (%.1fx without the identity hash; G2 %s), outcome/evidence G3 %s, "
1318 "end to end %.2fx (G6 %s)\n", s ? "merge_two_towers_3_3" : "reverse_tower_6", G.ops_ratio[s],
1319 G.w_g1[s] ? "PASS" : "FAIL", G.cpu_ratio[s], G.cpu_ratio_no_ident[s], G.w_g2[s] ? "PASS" : "FAIL",
1320 G.w_g3[s] ? "PASS" : "FAIL", G.e2e_ratio[s], G.w_g6[s] ? "PASS" : "FAIL");
1321 printf("task wording (at least one repeated workload): %s\n", G.task_gate ? "MET" : "NOT MET");
1322}
1323
1324int main(int argc, char **argv) {
1325 (void)argc;
1326 (void)argv;
1327 signal(SIGPIPE, SIG_IGN);
1328 place();
1329 perf_open();
1330 R.perf_ok = g_perf >= 0;
1331 memset(&g_skills, 0, sizeof g_skills);
1332 rx_plan_cache_init(&g_cache);
1333 g_a = calloc(1, sizeof *g_a);
1334 g_b = calloc(1, sizeof *g_b);
1335 if (!g_a || !g_b) return 2;
1336 t_repeated(SHAPE_REVERSE);
1337 t_repeated(SHAPE_MERGE);
1338 dump_cache("after repeated");
1339 t_identity();
1340 t_adversarial();
1341 dump_cache("after adversarial");
1342 t_rederive();
1343 dump_cache("after rederive");
1344 t_adapt();
1345 dump_cache("after adapt");
1346 if (!getenv("PLAN_REUSE_NO_ENERGY")) t_energy();
1347 printf("checks %d failures %d\n", g_checks, g_fail);
1348 write_receipt();
1349 rx_plan_cache_free(&g_cache);
1350 return g_fail ? 1 : 0;
1351}