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 / src / runtime / rx_plan.h

312 lines · 13182 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 * rx_plan.h -- Omega plan IR and verified plan cache (OMEGA_PLAN_REUSE).
19 *
20 * A plan is semantic computation: an action graph (rx_graph.h) whose World
21 * objects are slots, together with the conditions under which it is valid
22 * and the evidence it must leave. It is not prose and not a hidden chain of
23 * thought.
24 *
25 * goal object -> goal shape -> candidate templates -> applicability
26 * -> bind slots -> capability / resource conditions -> execute -> verify
27 *
28 * When nothing applies, AIEN plans (a domain planner, rx_plan_arrange.h);
29 * the plan is executed and verified, and its generalisation is stored as a
30 * CANDIDATE template. A CANDIDATE reuse first runs the graph's sequential
31 * reference on a copy of the World; a verified reuse promotes it to
32 * VERIFIED, which skips the reference. A reuse that does not verify marks it
33 * STALE and counts as a false applicability.
34 *
35 * Plan reuse is never blind replay. Every retrieval is checked against the
36 * goal's constraints, the World state the plan needs, the principal's
37 * authority (validated through the World's authority view, never minted),
38 * the resources the body offers now, the environment the plan assumes, and
39 * the World and cognitive generations it was verified under. Each accept or
40 * refusal is published to a World decision object, so it leaves a crumb.
41 *
42 * Identity. semantic_id is SHA-256 over canonical bytes of the semantic
43 * fields only (predicate lists sorted, the graph by its builder-order
44 * independent digest). Ancestry, verification status and counters are not
45 * semantics. A realization (one binding in one World, the capabilities
46 * used, the compiled graph, the World generation and core class) has its
47 * own realization_id; many realizations share one semantic id.
48 *
49 * The store is host-side. It takes no admin handle and cannot mint.
50 */
51#ifndef RX_PLAN_H
52#define RX_PLAN_H
53
54#include "rx_graph.h"
55#include "rx_world.h"
56
57#include <stdint.h>
58
59#define PL_MAX_SLOTS 8u /* AgGoal carries 8 World arguments */
60#define PL_MAX_PREDS 48u
61#define PL_MAX_TEMPLATES 32u
62
63/* Object types reserved for plans (0x5B01..0x5B0F). */
64enum {
65 PL_OT_UNIT = 0x5B01u, /* arrangement domain unit (rx_plan_arrange.h) */
66 PL_OT_WORLDGEN, /* 0 World generation */
67 PL_OT_COGNITION, /* 0 cognitive generation, 1 planner model id */
68 PL_OT_STATION, /* environment: 0 arm ready */
69 PL_OT_GOAL, /* 0 seq, 1 kind, 2 max steps, 3 energy budget, 4 facts ref */
70 PL_OT_DECISION, /* see rx_plan_decide */
71 PL_OT_INDEX, /* see rx_plan_publish_index */
72 PL_OT_GOALFACTS /* eight packed goal facts */
73};
74
75/* Named environment objects, in PlEnv.obj order. */
76enum { PL_ENV_GOAL = 0, PL_ENV_WORLDGEN, PL_ENV_COGNITION, PL_ENV_STATION, PL_ENV_FACTS,
77 PL_ENV_COUNT };
78
79/* Packed reference stored in a field: bit 63 set, generation, id. 0 = none. */
80static inline uint64_t pl_ref_pack(RxObjRef r) {
81 return (1ull << 63) | ((uint64_t)r.generation << 32) | r.id;
82}
83static inline RxObjRef pl_ref_unpack(uint64_t v) {
84 return (RxObjRef){ (uint32_t)(v & 0xffffffffu), (uint32_t)((v >> 32) & 0x7fffffffu) };
85}
86
87typedef enum {
88 PL_P_EQ_CONST = 1, /* slot.field == value */
89 PL_P_EQ_SLOT, /* slot.field == packed ref of slot2 */
90 PL_P_NOT_SLOT, /* slot.field != 0 and names no slot */
91 PL_P_UNREFERENCED, /* no live object of `type` outside the slots has field == ref(slot) */
92 PL_P_SELF_REF, /* slot.field == packed ref of slot itself */
93 PL_P_LIVE, /* slot is live at its bound generation and of `type` */
94 PL_P_ENV_EQ, /* env[slot].field == value */
95 PL_P_ENV_GE /* env[slot].field >= value */
96} PlPredKind;
97
98typedef struct {
99 uint32_t kind, slot, field, slot2, type;
100 uint64_t value;
101} PlPred;
102
103typedef struct {
104 uint32_t n;
105 PlPred p[PL_MAX_PREDS];
106} PlPredList;
107
108enum { PL_ROLE_GOAL = 1, PL_ROLE_INVOLVED };
109
110/* How an involved slot is found in a World: the unique live object of the
111 * slot's type whose field `field` holds the packed ref of slot `from`. */
112enum { PL_BIND_FROM_GOAL = 0, PL_BIND_RESTS_ON = 1 };
113
114typedef struct {
115 uint32_t type;
116 uint32_t role;
117 uint32_t rights; /* capability rights the plan needs on this object */
118 uint32_t bind_rule, bind_from, bind_field;
119} PlSlot;
120
121enum { PL_STATUS_CANDIDATE = 1, PL_STATUS_VERIFIED, PL_STATUS_STALE };
122enum { PL_ORIGIN_SEARCH = 1, PL_ORIGIN_ADAPTED, PL_ORIGIN_REDERIVED };
123enum { PL_GRAPH_RX_GRAPH = 1 }; /* action_graph kind */
124enum { PL_FAIL_GRAPH = 1u, PL_FAIL_INVARIANT = 2u, PL_FAIL_EVIDENCE = 4u, PL_FAIL_CRUMBS = 8u };
125
126/* Applicability axes, in check order. The first failing one is reported. */
127typedef enum {
128 PL_AX_OK = 0,
129 PL_AX_SHAPE,
130 PL_AX_BINDING,
131 PL_AX_GOAL,
132 PL_AX_WORLD_GEN,
133 PL_AX_COG_GEN,
134 PL_AX_ENVIRONMENT,
135 PL_AX_STATE,
136 PL_AX_AUTHORITY,
137 PL_AX_RESOURCES,
138 PL_AX_STALE, /* template is STALE */
139 PL_AX_COUNT
140} PlAxis;
141
142typedef struct {
143 uint32_t origin; /* PL_ORIGIN_* */
144 uint32_t has_parent;
145 uint8_t parent[32]; /* semantic id of the template this came from */
146 uint64_t goal_seq; /* goal that produced it */
147 uint64_t cog_gen; /* cognitive generation that produced it */
148} PlAncestry;
149
150typedef struct {
151 /* ---- semantic (hashed) ---- */
152 uint32_t goal_kind;
153 uint8_t goal_shape[32];
154 uint32_t n_goal_slots; /* slots 0..n-1 bound from the goal */
155 uint32_t n_slots;
156 PlSlot slots[PL_MAX_SLOTS];
157 PlPredList pre; /* over the goal / environment objects */
158 PlPredList state; /* required World state over slots */
159 PlPredList env; /* environment assumptions */
160 PlPredList invariants; /* before and after */
161 PlPredList success;
162 uint32_t fail_flags; /* failure predicate */
163 RxResourceNeed step_need; /* per step */
164 uint32_t n_steps;
165 uint64_t energy_total;
166 uint32_t n_evidence; /* evidence nodes after compile */
167 uint32_t graph_kind;
168 uint8_t graph_digest[32];
169 AgGraph graph; /* the template: World objects are params 1..n_slots */
170 /* ---- not semantic ---- */
171 uint8_t semantic_id[32];
172 PlAncestry ancestry;
173 uint32_t status;
174 uint64_t verified_world_gen, verified_cog_gen;
175 uint64_t uses, successes, failures, promotions, rederivations;
176 uint64_t refusals[PL_AX_COUNT];
177} PlanTemplate;
178
179/* Canonical semantic bytes. Returns length, or 0 when buf is too small. */
180size_t rx_plan_encode(const PlanTemplate *t, uint8_t *buf, size_t cap);
181/* Decode the semantic part (everything but the graph body, which is carried
182 * by its digest). 0 on success. */
183int rx_plan_decode(const uint8_t *buf, size_t n, PlanTemplate *t);
184/* Fill graph_digest (rx_graph_identify of the unbound template) and semantic_id. */
185void rx_plan_identify(PlanTemplate *t);
186void rx_plan_sort(PlanTemplate *t); /* canonical order of every predicate list */
187
188/* ---- World views ---- */
189
190typedef struct {
191 RxObjRef obj[PL_ENV_COUNT];
192} PlEnv;
193
194/* A consistent copy of every object slot, taken under the world lock. */
195typedef struct {
196 uint32_t n;
197 struct { uint32_t type, generation; uint8_t live; uint64_t resource;
198 uint64_t field[RX_MAX_FIELDS]; } o[RX_MAX_OBJECTS];
199} PlView;
200
201void rx_plan_view(RxWorld *w, PlView *v);
202
203/* What cognition cost. */
204typedef struct {
205 uint64_t shape_ops; /* goal-shape derivation steps */
206 uint64_t candidates; /* templates retrieved */
207 uint64_t preds; /* predicates evaluated */
208 uint64_t bind_attempts;
209 uint64_t expanded, generated, heuristic; /* planner */
210} PlCost;
211
212static inline uint64_t pl_cost_ops(const PlCost *c) {
213 return c->shape_ops + c->candidates + c->preds + c->bind_attempts + c->expanded +
214 c->generated + c->heuristic;
215}
216
217/* ---- checks ---- */
218
219/* 1 when the predicate holds. slots may be NULL for environment predicates. */
220int rx_plan_pred(const PlPred *p, const PlView *v, const PlEnv *env, const RxObjRef *slots,
221 uint32_t n_slots);
222/* Index of the first failing predicate, or -1 when all hold. */
223int rx_plan_preds(const PlPredList *l, const PlView *v, const PlEnv *env, const RxObjRef *slots,
224 uint32_t n_slots, PlCost *cost);
225
226/* Every applicability axis except shape (checked by retrieval), in PlAxis
227 * order. *pred_out receives the failing predicate index (or slot) when
228 * useful. The authority axis validates each needed capability through the
229 * World's authority view; nothing is minted. */
230PlAxis rx_plan_applicable(const PlanTemplate *t, RxWorld *w, const PlView *v, const PlEnv *env,
231 const RxObjRef *slots, const AgCapTable *caps, PlCost *cost,
232 int *pred_out);
233
234/* ---- store ---- */
235
236typedef struct {
237 uint32_t n;
238 PlanTemplate *t[PL_MAX_TEMPLATES];
239 uint64_t stored, deduplicated;
240} PlCache;
241
242void rx_plan_cache_init(PlCache *c);
243void rx_plan_cache_free(PlCache *c);
244/* Store a verified first execution. When a template with the same semantic id
245 * exists, its verification record is renewed (re-derivation) and no copy is
246 * made. Returns the stored template. */
247PlanTemplate *rx_plan_cache_store(PlCache *c, const PlanTemplate *t, uint64_t world_gen,
248 uint64_t cog_gen);
249PlanTemplate *rx_plan_cache_find(PlCache *c, const uint8_t semantic_id[32]);
250/* Templates of this shape and kind, in store order. */
251uint32_t rx_plan_cache_retrieve(PlCache *c, uint32_t goal_kind, const uint8_t shape[32],
252 PlanTemplate **out, uint32_t max, PlCost *cost);
253
254/* ---- execution ---- */
255
256typedef struct {
257 RxCapRef cell_cap, run_cap, ext_run_cap;
258 uint64_t cell_res, run_res;
259 const AgSkillTable *skills;
260 uint64_t core_class; /* recorded in the realization */
261 int reference_first; /* CANDIDATE: run the sequential reference first */
262} PlExecCtx;
263
264/* Compiled graph and its lowering. Both must outlive the world's reactions. */
265typedef struct { AgGraph g; AgLowered L; } PlRunStore;
266
267typedef int (*PlLegalFn)(const PlView *v); /* domain World legality */
268
269typedef struct {
270 int compile_verdict;
271 uint32_t n_missing, n_resource_blocked;
272 int reference_outcome; /* AG_RUN_* when run, 0 when not */
273 int outcome; /* graph AG_RUN_* */
274 uint32_t fail; /* PL_FAIL_* that held */
275 int success; /* success predicate held and no failure flag */
276 uint32_t nodes, evidence_required, evidence_present;
277 uint64_t crumbs_before, crumbs_after, crumbs_checked;
278 uint8_t realization_id[32];
279 uint8_t compiled_digest[32];
280 uint64_t compile_ns, run_ns, verify_ns, reference_ns;
281} PlExecResult;
282
283/* Compile the template with `slots` bound, optimise, lower into caller
284 * storage rs, run once as run id
285 * `run`, wait, collect, and verify. Checks nothing about applicability. */
286int rx_plan_execute(const PlanTemplate *t, RxWorld *w, const PlEnv *env, const RxObjRef *slots,
287 const AgCapTable *caps, const PlExecCtx *x, uint64_t run, PlRunStore *rs,
288 PlLegalFn legal, PlExecResult *out);
289
290/* After a reuse: success promotes CANDIDATE to VERIFIED; failure marks STALE. */
291void rx_plan_record_use(PlanTemplate *t, int success);
292
293/* ---- World records ---- */
294
295/* Decision fields: 0 goal seq, 1 axis (0 = accepted), 2..3 semantic id words
296 * 0..1 (0 when no candidate), 4 failing predicate + 1 (0 none), 5 decision
297 * seq, 6 realization id word 0 (accepted and run), 7 status after. Published
298 * as an outside publication; returns the crumb id or a negative error. */
299int64_t rx_plan_decide(RxWorld *w, RxCapRef cap, RxObjRef decision, uint64_t goal_seq,
300 PlAxis axis, const PlanTemplate *t, int pred, uint64_t seq,
301 const uint8_t *realization_id);
302
303/* Index fields: 0 templates stored, 1..2 semantic id words 0..1, 3 status,
304 * 4 verified World generation, 5 uses, 6 successes, 7 failures. */
305int64_t rx_plan_publish_index(RxWorld *w, RxCapRef cap, RxObjRef index, const PlCache *c,
306 const PlanTemplate *t);
307
308uint64_t pl_word(const uint8_t id[32], uint32_t k);
309const char *rx_plan_axis_name(PlAxis a);
310const char *rx_plan_status_name(uint32_t s);
311
312#endif /* RX_PLAN_H */