Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/physics0/pd0_world.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / physics0 / pd0_world.c` 241 lines ยท 10588 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 /* PD-0 world process side (spec 2.2, 2.3, 2.5): describe / reset / step with 18 * budgets, the STAND_IN range guard (pd0_guard), the STAND_IN recorder (one 19 * PD0REC1 record per call, SHA-256 chained), and the framed pipe server. */ 20 #define PD0_WORLD_IMPL 1 21 #include "physics0/pd0_world.h" 22 23 #include 24 25 #include "physics0/pd0_guard.h" 26 27 int pd0_world_init(pd0_world *w, int level, uint64_t seed) { 28 memset(w, 0, sizeof *w); 29 if (pd0_gen_desc(level, &w->desc) != 0 || pd0_gen_init(&w->gen, level, seed) != 0) return -1; 30 w->seed = seed; 31 pd0_stream(&w->noise, seed, "noise"); 32 pd0_stream(&w->null, seed, "null"); 33 return 0; 34 } 35 36 void pd0_world_describe(const pd0_world *w, pd0_desc *d) { *d = w->desc; } 37 38 static void begin_rec(pd0_world *w, pd0_rec *r, uint8_t kind, uint8_t channel) { 39 memset(r, 0, sizeof *r); 40 r->n_obs = w->desc.n_obs; 41 r->kind = kind; 42 r->channel = channel; 43 r->seq = w->seq; 44 r->episode = w->episode; 45 r->step_in_episode = w->step_in_episode; 46 r->time_micro = (int64_t)w->step_in_episode * w->desc.dt_micro; 47 memcpy(r->prev_hash, w->last_hash, PD0_HASH); 48 } 49 50 /* STAND_IN recorder: seal the record into the chain */ 51 static void seal(pd0_world *w, pd0_rec *r) { 52 uint8_t buf[PD0_REC_MAX]; 53 pd0_rec_encode(r, buf, sizeof buf); 54 memcpy(w->last_hash, r->hash, PD0_HASH); 55 w->seq++; 56 } 57 58 static int budget_spent(const pd0_world *w) { 59 return w->steps_used >= w->desc.budget_steps || w->episodes_used >= w->desc.budget_episodes; 60 } 61 62 void pd0_world_reset(pd0_world *w, const int64_t *vals, uint8_t n_vals, pd0_rec *r) { 63 begin_rec(w, r, PD0_KIND_RESET, PD0_CH_NONE); 64 for (int i = 0; i < w->desc.n_obs; i++) { r->before[i] = w->s[i]; r->after[i] = w->s[i]; } 65 if (budget_spent(w)) { r->status = PD0_BUDGET_EXHAUSTED; seal(w, r); return; } 66 w->episodes_used++; 67 if (w->in_episode || w->seq) w->episode++; 68 r->episode = w->episode; 69 if (!pd0_guard_reset_ok(&w->desc, vals, n_vals)) { /* STAND_IN guard: refused, still charged */ 70 r->status = PD0_REFUSED_RANGE; 71 w->in_episode = 0; 72 seal(w, r); 73 return; 74 } 75 for (int i = 0; i < w->desc.n_obs; i++) w->s[i] = vals[i]; 76 w->h = 0; 77 w->in_episode = 1; 78 w->step_in_episode = 0; 79 r->step_in_episode = 0; 80 r->time_micro = 0; 81 int64_t obs[PD0_MAX_OBS]; 82 pd0_gen_observe_reset(&w->gen, w->s, obs, &w->noise); 83 for (int i = 0; i < w->desc.n_obs; i++) { r->before[i] = obs[i]; r->after[i] = obs[i]; } 84 r->status = PD0_OK; 85 seal(w, r); 86 } 87 88 void pd0_world_step(pd0_world *w, uint8_t channel, int64_t value, pd0_rec *r) { 89 begin_rec(w, r, PD0_KIND_STEP, channel); 90 r->requested = value; 91 int64_t obs_before[PD0_MAX_OBS]; 92 pd0_gen_observe_reset(&w->gen, w->s, obs_before, &w->noise); 93 for (int i = 0; i < w->desc.n_obs; i++) { r->before[i] = obs_before[i]; r->after[i] = obs_before[i]; } 94 if (budget_spent(w) || !w->in_episode) { r->status = PD0_BUDGET_EXHAUSTED; seal(w, r); return; } 95 w->steps_used++; 96 if (!pd0_guard_step_ok(&w->desc, channel, value)) { /* STAND_IN guard: refused, still charged */ 97 r->status = PD0_REFUSED_RANGE; 98 seal(w, r); 99 return; 100 } 101 int64_t u[PD0_MAX_CH] = {0}; 102 if (channel != PD0_CH_NONE) u[channel] = value; 103 r->applied = channel == PD0_CH_NONE ? 0 : value; 104 int64_t ns[PD0_MAX_OBS], nh, obs[PD0_MAX_OBS]; 105 int bad = pd0_gen_step(&w->gen, w->s, w->h, u, w->desc.dt_micro, ns, &nh, obs, &w->noise, &w->null); 106 w->step_in_episode++; 107 if (bad) { 108 r->status = PD0_OUT_OF_BOUNDS; /* nothing revealed: after == before */ 109 w->in_episode = 0; 110 seal(w, r); 111 return; 112 } 113 for (int i = 0; i < w->desc.n_obs; i++) { w->s[i] = ns[i]; r->after[i] = obs[i]; } 114 w->h = nh; 115 r->status = PD0_OK; 116 if (w->step_in_episode >= w->desc.episode_max_steps) { r->status = PD0_EPISODE_END; w->in_episode = 0; } 117 seal(w, r); 118 } 119 120 /* ---- protocol v2 (docs/physics0/PD0_PROTOCOL_V2.md) ---- */ 121 /* score: noise-free truth trajectory. Stateless: no budget, no hash chain, no guard, no RNG draw. */ 122 static size_t handle_score(const pd0_world *w, const uint8_t *req, size_t len, uint8_t *resp, size_t cap) { 123 if (len < 3 || req[1] != w->desc.n_obs) return 0; 124 unsigned no = req[1]; 125 if (len < 3 + 8u * no) return 0; 126 unsigned n = req[2 + 8 * no]; 127 if (n > PD0_SCORE_STEPS_MAX || len != 3 + 8u * no + 9u * n || cap < 2 + 8u * no * n) return 0; 128 int64_t s[PD0_MAX_OBS] = {0}, h = 0, ns[PD0_MAX_OBS], nh, obs[PD0_MAX_OBS]; 129 for (unsigned j = 0; j < no; j++) s[j] = (int64_t)pd0_get_u64(req + 2 + 8 * j); 130 pd0_rng dn, dl; 131 pd0_stream(&dn, 1, "x"); pd0_stream(&dl, 1, "y"); /* scratch streams: the returned state never depends on them */ 132 const uint8_t *sp = req + 3 + 8 * no; 133 unsigned done = 0, status = PD0_OK; 134 uint8_t *out = resp + 2; 135 for (unsigned st = 0; st < n; st++, sp += 9) { 136 uint8_t ch = sp[0]; 137 if (ch != PD0_CH_NONE && ch >= w->desc.n_channels) return 0; 138 int64_t u[PD0_MAX_CH] = {0}; 139 if (ch != PD0_CH_NONE) u[ch] = (int64_t)pd0_get_u64(sp + 1); 140 if (pd0_gen_step(&w->gen, s, h, u, w->desc.dt_micro, ns, &nh, obs, &dn, &dl)) { status = PD0_OUT_OF_BOUNDS; break; } 141 memcpy(s, ns, sizeof s); h = nh; 142 for (unsigned j = 0; j < no; j++) { pd0_put_u64(out, (uint64_t)s[j]); out += 8; } 143 done++; 144 } 145 resp[0] = (uint8_t)status; resp[1] = (uint8_t)done; 146 return (size_t)(out - resp); 147 } 148 149 /* shape: the relation shape and the true terms, in PD0SHAP1 form */ 150 static size_t handle_shape(const pd0_world *w, uint8_t *resp, size_t cap, int full) { 151 pd0_relation r; 152 memset(&r, 0, sizeof r); 153 if (pd0_gen_true_relation(&w->gen, w->desc.dt_micro, &r) < 0 || r.n_vars > PD0_MAX_VARS) return 0; 154 unsigned nx = r.n_vars + r.n_channels, nt = 0; 155 for (int e = 0; e < r.n_eq; e++) nt += r.eq[e].n_terms; 156 if (!full) nt = 0; /* before final: dimensions only, zero terms */ 157 size_t need = 8 + 4 + 8 + 2 + (size_t)nt * (1 + 8 + nx); 158 if (cap < need) return 0; 159 uint8_t *p = resp; 160 memcpy(p, "PD0SHAP1", 8); p += 8; 161 *p++ = r.n_vars; *p++ = r.n_latent; *p++ = r.n_channels; *p++ = full ? r.n_eq : 0; /* n_eq and relation size are withheld until final */ 162 pd0_put_u32(p, (uint32_t)pd0_gen_true_size(w->gen.level)); p += 4; 163 pd0_put_u32(p, full ? (uint32_t)pd0_relation_size(&r) : 0u); p += 4; 164 pd0_put_u16(p, (uint16_t)nt); p += 2; 165 for (int e = 0; full && e < r.n_eq; e++) for (int t = 0; t < r.eq[e].n_terms; t++) { 166 *p++ = r.eq[e].target; 167 pd0_put_u64(p, (uint64_t)r.eq[e].t[t].coef); p += 8; 168 for (int i = 0; i < r.n_vars; i++) *p++ = r.eq[e].t[t].ex[i]; 169 for (int c = 0; c < r.n_channels; c++) *p++ = r.eq[e].t[t].ex[PD0_MAX_VARS + c]; 170 } 171 return (size_t)(p - resp); 172 } 173 174 /* audit: final gate state and refusal counters; free at all times, never counted */ 175 static size_t handle_audit(const pd0_world *w, uint8_t *resp, size_t cap) { 176 if (cap < PD0_AUDIT_RESP_SIZE) return 0; 177 uint8_t *p = resp; 178 memcpy(p, "PD0AUDT1", 8); p += 8; 179 *p++ = (uint8_t)(w->final_done ? 1 : 0); 180 pd0_put_u32(p, w->n_score_before_final); p += 4; 181 pd0_put_u32(p, w->n_shape_before_final); p += 4; 182 pd0_put_u32(p, w->n_play_after_final); p += 4; 183 pd0_put_u32(p, w->n_dup_final); p += 4; 184 memcpy(p, w->final_hash, PD0_HASH); p += PD0_HASH; 185 return (size_t)(p - resp); 186 } 187 188 size_t pd0_world_handle(pd0_world *w, const uint8_t *req, size_t len, uint8_t *resp, size_t cap) { 189 if (len < 1) return 0; 190 pd0_rec r; 191 switch (req[0]) { 192 case PD0_OP_DESCRIBE: 193 return len == 1 ? pd0_desc_encode(&w->desc, resp, cap) : 0; 194 case PD0_OP_RESET: { 195 if (w->final_done) { w->n_play_after_final++; return 0; } 196 if (len < 2) return 0; 197 uint8_t n = req[1]; 198 if (n > PD0_MAX_OBS || len != 2 + 8u * n) return 0; 199 int64_t vals[PD0_MAX_OBS] = {0}; 200 for (unsigned i = 0; i < n; i++) vals[i] = (int64_t)pd0_get_u64(req + 2 + 8 * i); 201 pd0_world_reset(w, vals, n, &r); 202 return pd0_rec_encode(&r, resp, cap); 203 } 204 case PD0_OP_STEP: 205 if (w->final_done) { w->n_play_after_final++; return 0; } 206 if (len != 10) return 0; 207 pd0_world_step(w, req[1], (int64_t)pd0_get_u64(req + 2), &r); 208 return pd0_rec_encode(&r, resp, cap); 209 case PD0_OP_SCORE: 210 if (!w->final_done) { w->n_score_before_final++; return 0; } 211 return handle_score(w, req, len, resp, cap); 212 case PD0_OP_SHAPE: 213 return len == 1 ? handle_shape(w, resp, cap, w->final_done) : 0; /* before final: dimensions only, not a refusal */ 214 case PD0_OP_FINAL: 215 if (len != 1 + PD0_HASH || cap < 1) return 0; 216 if (w->final_done) { w->n_dup_final++; resp[0] = 1; return 1; } /* duplicate: refused, first hash kept */ 217 memcpy(w->final_hash, req + 1, PD0_HASH); w->final_done = 1; resp[0] = 0; 218 return 1; 219 case PD0_OP_AUDIT: return len == 1 ? handle_audit(w, resp, cap) : 0; 220 default: return 0; 221 } 222 } 223 224 size_t pd0_world_chan(void *ctx, const uint8_t *req, size_t len, uint8_t *resp, size_t cap) { 225 return pd0_world_handle((pd0_world *)ctx, req, len, resp, cap); 226 } 227 228 int pd0_world_serve(pd0_world *w, int in_fd, int out_fd) { 229 static uint8_t req[PD0_REQ_MAX_V2], resp[PD0_SCORE_RESP_MAX > PD0_SHAPE_RESP_MAX ? PD0_SCORE_RESP_MAX : PD0_SHAPE_RESP_MAX]; 230 uint8_t len4[4]; 231 for (;;) { 232 if (pd0_read_full(in_fd, len4, 4) != 0) return 0; /* EOF: learner closed */ 233 uint32_t n = pd0_get_u32(len4); 234 if (n == 0 || n > sizeof req) return -1; 235 if (pd0_read_full(in_fd, req, n) != 0) return -1; 236 size_t rn = pd0_world_handle(w, req, n, resp, sizeof resp); 237 pd0_put_u32(len4, (uint32_t)rn); 238 if (pd0_write_full(out_fd, len4, 4) != 0) return -1; 239 if (rn && pd0_write_full(out_fd, resp, rn) != 0) return -1; 240 } 241 }