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/runtime/rx_jspace.h # 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 / runtime / rx_jspace.h` 529 lines ยท 26284 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_jspace.h -- branch-native shared cognitive state for J-Space. 19 * 20 * Two layers, kept apart on purpose. 21 * 22 * Semantic layer. A branch is an ordered sequence of state units. Each unit has 23 * a semantic identity: a SHA-256 chain over the semantic events that produced it 24 * (root seed, a derive step with its input token, an edit with its offset and 25 * patch). Identity never mentions a realization type, bytes, placement or 26 * residency. Two branches that took the same semantic steps hold the same 27 * semantic identities, whatever their physical state. 28 * 29 * Physical layer. A realization is one way the bytes of one semantic unit are 30 * held right now: a native latent checkpoint, a neural activation checkpoint, 31 * attention/KV state, compiled features, a World or Cortex projection, or a 32 * physical tensor. A realization records its recipe (how to rebuild it from its 33 * parent realization), its placement and its measured costs. Branches share 34 * realizations by reference; a realization referenced by more than one holder 35 * is immutable. Writing through a branch to a shared realization copies it (or 36 * rebuilds it) into a private one first. A branch that has children is frozen. 37 * 38 * The FORGE branch policy chooses reuse, copy, reference, recompute, move, 39 * compress, spill or evict by comparing measured costs. It never changes a 40 * semantic identity and it never changes the bytes a branch reads. 41 * 42 * Realizers are a vtable. Nothing in the store or the policy depends on which 43 * representation a realizer uses. 44 */ 45 #ifndef RX_JSPACE_H 46 #define RX_JSPACE_H 47 48 #include 49 #include 50 #include 51 52 #define JS_OK 0 53 #define JS_ERR_ARG -1 54 #define JS_ERR_FULL -2 55 #define JS_ERR_FROZEN -3 /* branch has children: its state is a shared ancestor */ 56 #define JS_ERR_IO -4 57 #define JS_ERR_CORRUPT -5 /* restored bytes disagree with the recorded content digest */ 58 #define JS_ERR_NOMEM -6 59 #define JS_ERR_STALE -7 /* handle generation is not current: the object was reclaimed */ 60 #define JS_ERR_REMOTE -8 /* not locally owned; this build has no remote transport */ 61 #define JS_ERR_OWNER -9 /* caller is not the recorded owner */ 62 #define JS_ERR_VERSION -10 /* durable checkpoint in a refused older format (v1: 32-bit realization generations) */ 63 64 #include 65 66 typedef struct { uint8_t b[32]; } JsSemId; 67 68 /* ---- production mechanics (M20) ------------------------------------------- 69 * 70 * Logical identity is separate from physical address. A realization has a 71 * stable JsRealId {slot, gen}; a branch has a stable JsBranchRef {id, gen}. 72 * Both survive js_space_commit / js_space_open unchanged. A slot is reused 73 * only after its generation advances, so a reference to a reclaimed object 74 * is refused with JS_ERR_STALE and never aliases the new occupant. The 75 * legacy uint32_t branch-id API stays: the id is the slot, and it is only 76 * safe while the caller still holds the branch. Code that keeps a branch 77 * beyond one call (World fields, other subsystems) keeps a JsBranchRef. 78 * 79 * Ownership rules (J-Space storage): 80 * - J-Space holds runtime state-space material only. Memory, evidence and 81 * provenance belong to Cortex; J-Space never records them. 82 * - A realization lives while a branch unit or a child recipe holds it 83 * (refs). Reclamation is immediate in memory; spill/patch extents that a 84 * durable checkpoint may still name are quarantined until the next 85 * js_space_commit makes a checkpoint that no longer names them. 86 * - A branch has one owner subject (0 = the space itself). Owner-checked 87 * release (js_branch_release_ref) refuses other subjects. 88 * - Shared logical state is published only through a World commit. A writer 89 * forks a STAGED branch, works on it privately, then publishes its packed 90 * JsBranchRef as a World field value through the World's own commit path. 91 * After the commit the branch is sealed (js_branch_seal); staged branches 92 * a World commit never named are reclaimed (js_space_reclaim_staged) and 93 * are never persisted. A branch a World field names is never mutated in 94 * place: once forked it is frozen, so older World generations keep reading 95 * the bytes they named. 96 * 97 * Generations never wrap. A realization generation is 64 bits: one slot 98 * reused once per nanosecond needs about 584 years to exhaust it, and if it 99 * ever were, the slot retires (JS_REAL_GEN_RETIRED) instead of wrapping. A 100 * branch generation stays 32 bits because a JsBranchRef packs into one 64-bit 101 * World field ({id, gen}); a branch slot whose next generation would be 102 * JS_BRANCH_GEN_RETIRED (UINT32_MAX) retires instead: it is never handed out 103 * again, in memory or after js_space_open, the rule World objects follow 104 * (rx_world_retire). A retired branch slot costs one of max_branches; when no 105 * slot is left, a new branch fails JS_ERR_FULL. Either way a reference to a 106 * reclaimed object never validates again. 107 * 108 * Concurrency: one space mutex (recursive) serializes every public call, 109 * matching the World reference's single world mutex. Realized bytes never 110 * change after publication, so a read returns the bytes of the generation 111 * the caller named. Raw JsReal pointers are only valid while a holder keeps 112 * the realization alive. 113 * 114 * Machine placement: JsHome is a placeholder for the canonical machine 115 * identity being defined by M20 Agent 1. `machine` is an opaque fixed-size 116 * byte field; J-Space never interprets it beyond equality. Fabric is not 117 * implemented: mutating a branch whose home is not JS_HOME_LOCAL, and 118 * reading non-resident units of a REMOTE_OWNED branch, fail JS_ERR_REMOTE. 119 */ 120 #define JS_MACHINE_ID_BYTES 32u /* == AIEN_MID_ID_BYTES; holds AienMachineId bytes (placement is still a placeholder) */ 121 122 typedef enum { 123 JS_HOME_LOCAL = 0, /* owned and authoritative on this machine */ 124 JS_HOME_REMOTE_OWNED = 1, /* authoritative elsewhere; no local writes */ 125 JS_HOME_REPLICA = 2 /* local read-only copy of a remote owner's branch */ 126 } JsLocality; 127 128 typedef struct { 129 uint8_t machine[JS_MACHINE_ID_BYTES]; /* opaque canonical machine identity bytes */ 130 uint32_t locality; /* JsLocality */ 131 } JsHome; 132 133 typedef struct { uint32_t slot; uint64_t gen; } JsRealId; 134 typedef struct { uint32_t id; uint32_t gen; } JsBranchRef; 135 136 /* Generation values that are never live (see "Generations never wrap"). */ 137 #define JS_REAL_GEN_RETIRED UINT64_MAX 138 #define JS_BRANCH_GEN_RETIRED UINT32_MAX 139 140 /* A branch reference fits one 64-bit World field. */ 141 static inline uint64_t js_branch_ref_pack(JsBranchRef r) { 142 return ((uint64_t)r.gen << 32) | r.id; 143 } 144 static inline JsBranchRef js_branch_ref_unpack(uint64_t v) { 145 JsBranchRef r = { (uint32_t)v, (uint32_t)(v >> 32) }; 146 return r; 147 } 148 149 /* Hard limits. Exceeding one fails deterministically with JS_ERR_FULL before 150 * anything changes. Zero for a byte limit means unlimited. */ 151 typedef struct { 152 uint32_t max_reals; /* live realizations (slab slots) */ 153 uint32_t max_branches; /* branch slots, at most JS_MAX_BRANCHES */ 154 uint64_t max_spill_bytes; /* extent space in the spill/data file */ 155 uint64_t max_resident_bytes; /* HOT + COLD + COMPRESSED bytes */ 156 } JsLimits; 157 158 typedef struct { uint64_t off, len; } JsExtent; 159 160 /* Realization types. Listed so receipts can name them; none is privileged. */ 161 typedef enum { 162 JS_REAL_LATENT_CHECKPOINT = 1, 163 JS_REAL_ACTIVATION_CHECKPOINT, 164 JS_REAL_KV_STATE, 165 JS_REAL_COMPILED_FEATURES, 166 JS_REAL_WORLD_PROJECTION, 167 JS_REAL_CORTEX_PROJECTION, 168 JS_REAL_PHYSICAL_TENSOR 169 } JsRealType; 170 171 /* Where the bytes are. Placement is not identity. */ 172 typedef enum { 173 JS_PLACE_HOT = 1, /* host arena A, uncompressed */ 174 JS_PLACE_COLD, /* host arena B, uncompressed (target of MOVE) */ 175 JS_PLACE_COMPRESSED, /* delta against parent realization, zero-run coded, in memory */ 176 JS_PLACE_SPILLED, /* uncompressed in the spill file */ 177 JS_PLACE_EVICTED /* no bytes anywhere; rebuilt from the recipe */ 178 } JsPlacement; 179 180 typedef enum { 181 JS_ACT_REUSE = 0, /* an existing realization of the same semantic unit and type */ 182 JS_ACT_COPY, /* private copy before a write */ 183 JS_ACT_REFERENCE, /* child branch points at the parent's realization */ 184 JS_ACT_RECOMPUTE, /* rebuild bytes from the recipe */ 185 JS_ACT_MOVE, /* relocate bytes to another arena */ 186 JS_ACT_COMPRESS, 187 JS_ACT_SPILL, 188 JS_ACT_EVICT, 189 JS_ACT_COUNT 190 } JsAction; 191 192 typedef enum { JS_RECIPE_ROOT = 1, JS_RECIPE_DERIVE, JS_RECIPE_EDIT } JsRecipeKind; 193 194 /* A representation. derive() must be a pure function of (prev, token): the same 195 * inputs give the same bytes. prev is NULL for the root, where token is the seed. */ 196 typedef struct { 197 JsRealType type; 198 const char *name; 199 size_t unit_bytes; 200 void (*derive)(const uint8_t *prev, uint64_t token, uint8_t *out, size_t n); 201 } JsRealizer; 202 203 typedef struct JsReal JsReal; 204 205 /* One realization of one semantic state unit. */ 206 struct JsReal { 207 JsSemId semantic_state_id; 208 JsRealType realization_type; 209 const JsRealizer *realizer; 210 JsReal *parent_realization; /* recipe input; held by reference */ 211 JsRecipeKind recipe; 212 uint64_t token; /* DERIVE input or ROOT seed */ 213 uint32_t edit_off, edit_len; /* EDIT patch */ 214 uint8_t *edit_bytes; 215 uint32_t refs; /* branches + child recipes + index entries holding it */ 216 uint32_t holders; /* branches holding it as a state unit */ 217 JsPlacement placement; 218 uint8_t *bytes; /* HOT/COLD: unit_bytes; else NULL */ 219 uint8_t *packed; /* COMPRESSED: coded bytes */ 220 size_t packed_len; 221 uint64_t spill_off; /* SPILLED: offset in the spill file */ 222 uint8_t content[32]; /* SHA-256 of the realized bytes, fixed at creation */ 223 uint64_t last_use; /* logical clock of the last read */ 224 uint64_t reads; 225 JsReal *index_next; /* semantic reuse index chain */ 226 /* M20: stable identity and durable recipe input. */ 227 uint32_t slot; /* JsRealId slot; the slab never moves a JsReal */ 228 uint64_t gen; /* JsRealId generation, never JS_REAL_GEN_RETIRED while live */ 229 uint64_t patch_off; /* EDIT patch extent in the data file, UINT64_MAX if none */ 230 JsReal *free_next; /* slab free list */ 231 }; 232 233 /* Measured costs, all in nanoseconds or bytes. The policy reads nothing else. */ 234 typedef struct { 235 double derive_ns; /* one derive of one unit, per realizer (set per call) */ 236 double copy_ns_per_byte; 237 double compress_ns_per_byte; 238 double decompress_ns_per_byte; 239 double spill_write_ns_per_byte; 240 double spill_read_ns_per_byte; 241 double move_ns_per_byte; 242 double retain_ns_per_byte; /* price of keeping one byte resident under pressure */ 243 double compress_ratio; /* measured packed/unpacked for this realizer's deltas */ 244 } JsCosts; 245 246 /* A branch as J-Space sees it. The state arrays are the physical view of the 247 * semantic sequence; SharedStateRealization below is the accounting view. */ 248 typedef struct { 249 uint32_t branch_id; 250 uint32_t parent_branch; /* UINT32_MAX for a root */ 251 uint32_t common_ancestor; /* root of this lineage */ 252 uint32_t divergence_point; /* number of units inherited at fork */ 253 bool frozen; /* has children */ 254 uint32_t n_units, cap_units; 255 JsReal **units; /* units[i] realizes semantic unit i */ 256 const JsRealizer *realizer; 257 /* M20 */ 258 uint32_t gen; /* JsBranchRef generation of this occupant */ 259 uint32_t owner; /* owner subject, 0 = the space */ 260 bool staged; /* not yet named by a World commit; never persisted */ 261 JsHome home; 262 } JsBranch; 263 264 /* The SharedStateRealization record of one branch, derived on demand. */ 265 typedef struct { 266 JsSemId semantic_state_id; /* the branch's semantic identity */ 267 JsRealType realization_type; 268 uint32_t parent_realization; /* parent branch id, UINT32_MAX for a root */ 269 uint32_t n_shared; /* units whose realization another holder also holds */ 270 uint32_t n_private; 271 uint64_t resident_bytes; /* bytes of this branch's units held in memory */ 272 uint32_t placement_count[6]; /* index by JsPlacement */ 273 double recompute_cost_ns, transfer_cost_ns, retain_cost_ns, eviction_cost_ns; 274 } JsSharedStateRealization; 275 276 typedef struct { 277 uint64_t actions[JS_ACT_COUNT]; 278 uint64_t derives; /* realizer derive() calls */ 279 uint64_t bytes_copied; 280 uint64_t live_reals; 281 uint64_t resident_bytes; /* HOT + COLD + COMPRESSED bytes */ 282 uint64_t peak_resident_bytes; 283 uint64_t spilled_bytes; 284 uint64_t corrupt_restores; 285 } JsStats; 286 287 /* DUAL-3a observer (declared in full after the policy section below). */ 288 typedef struct JsDualObservation_ JsDualObservation; 289 typedef void (*JsDualObserver)(const JsDualObservation *obs, const void *ctx); 290 291 #define JS_MAX_BRANCHES 4096u 292 #define JS_INDEX_BUCKETS 65536u 293 294 typedef struct { 295 JsBranch *branches[JS_MAX_BRANCHES]; 296 uint32_t n_branches; 297 JsReal *index[JS_INDEX_BUCKETS]; 298 JsCosts costs; 299 JsStats stats; 300 uint64_t clock; 301 int spill_fd; 302 uint64_t spill_end; 303 bool reuse_enabled; 304 /* Bytes of the last realization rebuilt, so a sequential read of a 305 * rebuilt chain does not replay the chain once per unit. Realized bytes 306 * never change, so the entry stays valid while that realization lives. */ 307 const JsReal *cache_r; 308 uint8_t *cache_bytes; 309 size_t cache_len; 310 /* ---- M20 production mechanics ---- */ 311 JsLimits limits; 312 pthread_mutex_t mu; /* recursive; serializes every public call */ 313 bool mu_ready; 314 uint32_t branch_gen[JS_MAX_BRANCHES]; /* JS_BRANCH_GEN_RETIRED = slot retired */ 315 uint32_t free_branch[JS_MAX_BRANCHES]; 316 uint32_t n_free_branch; 317 JsReal **slab; /* chunks of JS_SLAB_CHUNK realizations */ 318 uint32_t slab_chunks; 319 uint32_t real_hw; /* slots ever handed out */ 320 uint64_t real_gen_floor; /* first generation of a never-used slot (saturates) */ 321 JsReal *real_free; 322 JsExtent *ext_free; /* free extents, sorted by offset, coalesced */ 323 uint32_t n_ext_free, cap_ext_free; 324 JsExtent *ext_pending; /* freed but maybe named by the last checkpoint */ 325 uint32_t n_ext_pending, cap_ext_pending; 326 bool durable; 327 char *dir; 328 uint64_t commit_seq; 329 uint8_t anchor[48]; /* opaque client anchor (v3 checkpoint) */ 330 bool anchor_set; 331 JsHome local_home; /* home given to locally created branches */ 332 /* DUAL-3a decision-site observer; NULL = no observation (default) */ 333 JsDualObserver dual_observer; 334 const void *dual_ctx; 335 uint64_t dual_seq; 336 } JsSpace; 337 338 #define JS_SLAB_CHUNK 1024u 339 340 /* Anonymous, volatile space: the spill file is unlinked at once. Default limits. */ 341 int js_space_init(JsSpace *s, const char *spill_path); 342 /* Same, with explicit limits (NULL = defaults). */ 343 int js_space_init_limits(JsSpace *s, const char *spill_path, const JsLimits *lim); 344 void js_space_destroy(JsSpace *s); 345 void js_limits_default(JsLimits *lim); 346 347 /* Durable space in directory `dir` (created if missing): data file 348 * jspace.data (spill + EDIT patches, fixed extents) and metadata checkpoint 349 * jspace.meta. If a checkpoint exists, it is validated and loaded: every 350 * branch, realization id and generation comes back; realizations reopen as 351 * SPILLED (if their extent was checkpointed) or EVICTED and rebuild from their 352 * recipe. `realizers` must name every realization type the checkpoint uses 353 * (matched by type and unit_bytes). A torn or corrupt checkpoint fails with 354 * JS_ERR_CORRUPT and leaves no state; a leftover temporary file is ignored. 355 * A version-1 checkpoint (32-bit realization generations) fails with 356 * JS_ERR_VERSION and leaves no state: it is refused, never reinterpreted. 357 * A version-2 checkpoint (no client anchor) still loads, with the anchor absent 358 * (js_space_anchor returns 0). Commits write version 3. */ 359 int js_space_open(JsSpace *s, const char *dir, const JsRealizer *const *realizers, 360 uint32_t n_realizers, const JsLimits *lim, const JsHome *local_home); 361 /* Make the current non-staged state the durable checkpoint: data first 362 * (fdatasync), then metadata to a temporary file, fsync, atomic rename, 363 * directory fsync. Only then are quarantined extents reusable. */ 364 int js_space_commit(JsSpace *s); 365 /* Opaque 48-byte client anchor stored in the checkpoint (format v3), covered 366 * by the body digest. js_space_set_anchor copies it into the space; the next 367 * js_space_commit writes it. js_space_anchor copies it out and returns 1 if one 368 * is loaded or set, 0 if absent (a version-2 checkpoint, never set, or an 369 * all-zero section: clients must make a real anchor non-zero). J-Space 370 * never interprets the bytes. */ 371 void js_space_set_anchor(JsSpace *s, const uint8_t anchor[48]); 372 int js_space_anchor(const JsSpace *s, uint8_t out[48]); 373 374 /* Stable references. */ 375 int js_branch_ref(JsSpace *s, uint32_t id, JsBranchRef *out); 376 int js_branch_check(JsSpace *s, JsBranchRef ref); /* JS_OK or JS_ERR_STALE */ 377 int js_real_id(JsSpace *s, uint32_t b, uint32_t idx, JsRealId *out); 378 /* NULL when the slot was reclaimed (generation mismatch). Valid while held. */ 379 JsReal *js_real_lookup(JsSpace *s, JsRealId id); 380 381 /* Ownership and staging. */ 382 int js_branch_fork_staged(JsSpace *s, JsBranchRef parent, uint32_t owner, JsBranchRef *out); 383 int js_branch_seal(JsSpace *s, JsBranchRef b); /* a World commit named it */ 384 int js_branch_release_ref(JsSpace *s, JsBranchRef b, uint32_t subject); 385 /* Release every branch still staged. Call when no commit can name them. */ 386 uint32_t js_space_reclaim_staged(JsSpace *s); 387 int js_branch_set_owner(JsSpace *s, JsBranchRef b, uint32_t owner); 388 int js_branch_set_home(JsSpace *s, JsBranchRef b, const JsHome *home); 389 int js_branch_home(JsSpace *s, JsBranchRef b, JsHome *out); 390 /* COMPOSITION-2: lifecycle facts of one branch, read under the space lock 391 * (JS_ERR_STALE for a reclaimed reference). parent_gen is the generation of 392 * the parent slot's current occupant, 0 when the branch is a root or the 393 * parent slot is empty. */ 394 typedef struct { 395 uint32_t owner; 396 uint32_t parent, parent_gen; 397 bool staged; 398 uint32_t locality; /* JsLocality */ 399 } JsBranchInfo; 400 int js_branch_info(JsSpace *s, JsBranchRef b, JsBranchInfo *out); 401 402 /* Measure the costs the policy reads, for this realizer on this machine. 403 * JS_ERR_ARG for a missing realizer or a zero unit size, JS_ERR_NOMEM when the 404 * scratch buffers cannot be allocated; s->costs is unchanged on either error. */ 405 int js_calibrate(JsSpace *s, const JsRealizer *r); 406 407 /* Semantic identities. */ 408 JsSemId js_sem_root(uint64_t seed); 409 JsSemId js_sem_derive(const JsSemId *prev, uint64_t token); 410 JsSemId js_sem_edit(const JsSemId *prev, uint32_t off, const uint8_t *patch, uint32_t len); 411 /* Identity of a whole branch: SHA-256 over its ordered unit identities. */ 412 JsSemId js_branch_semantic_id(const JsBranch *b); 413 414 /* Branch operations. */ 415 int js_branch_root(JsSpace *s, const JsRealizer *r, uint64_t seed, uint32_t *out); 416 int js_branch_fork(JsSpace *s, uint32_t parent, uint32_t *out); 417 /* Append one unit derived from the last one. */ 418 int js_branch_derive(JsSpace *s, uint32_t b, uint64_t token); 419 /* Overwrite len bytes of unit idx. Never touches a realization another holder has. */ 420 int js_branch_edit(JsSpace *s, uint32_t b, uint32_t idx, uint32_t off, 421 const uint8_t *patch, uint32_t len); 422 /* Read unit idx into out (unit_bytes). Restores the bytes if they are not resident. */ 423 int js_branch_read(JsSpace *s, uint32_t b, uint32_t idx, uint8_t *out); 424 /* SHA-256 over every unit's realized bytes, in order. Restores as needed. */ 425 int js_branch_content_digest(JsSpace *s, uint32_t b, uint8_t out[32]); 426 int js_branch_release(JsSpace *s, uint32_t b); 427 void js_branch_realization(JsSpace *s, uint32_t b, JsSharedStateRealization *out); 428 429 /* Direct physical operations (the policy calls these; tests may too). None of 430 * them changes a semantic identity or the bytes a later read returns. */ 431 int js_real_move(JsSpace *s, JsReal *r); 432 int js_real_compress(JsSpace *s, JsReal *r); 433 int js_real_spill(JsSpace *s, JsReal *r); 434 int js_real_evict(JsSpace *s, JsReal *r); 435 int js_real_restore(JsSpace *s, JsReal *r); /* back to HOT; RECOMPUTE when evicted */ 436 437 /* FORGE branch policy. Choose the cheapest way to bring resident bytes under 438 * budget: for each candidate it compares expected restore cost against the 439 * retain price, using only JsCosts and the realization's own history. */ 440 typedef struct { 441 uint64_t considered; 442 uint64_t chosen[JS_ACT_COUNT]; 443 uint64_t kept; /* candidates for which keeping was cheapest */ 444 } JsPolicyReport; 445 JsAction js_forge_choose(const JsSpace *s, const JsReal *r, double pressure); 446 int js_forge_enforce(JsSpace *s, uint64_t budget_bytes, JsPolicyReport *rep); 447 448 /* ---- DUAL decision site "jspace.residency" (ADR 0031 section 7.3, DUAL-3a) -- 449 * 450 * A read-only observation tap on the residency decision inside 451 * js_forge_enforce. When an observer is set, each candidate the chooser 452 * decides on emits one JsDualObservation: the alternatives it priced (every 453 * action with the cost inputs and the score the chooser computed, and whether 454 * the action was admissible), the chosen action, the pressure value and which 455 * case produced it (over total budget: CAPACITY, stays hard; hot arena over 456 * half the budget: SOFT), the realization's identity, and the budget and 457 * residency bytes at that instant. The observer receives a const record and 458 * returns nothing: it cannot alter the decision by type. With no observer (the 459 * default) the policy runs exactly as before and no record is built. The 460 * observer runs under the space lock and must not mutate the space or any 461 * realization. There is no wall-clock field: seq is a per-space logical 462 * counter that only advances while an observer is set. 463 * 464 * Canonical encoding (js_dual_observation_encode): fixed little-endian layout 465 * of every field but digest, JS_DUAL_OBS_BYTES long, kind byte and version 466 * first so the record is self-describing. digest = SHA-256 over 467 * JS_DUAL_OBS_DOMAIN, one zero byte, then the canonical bytes. */ 468 #define JS_DUAL_SITE_ID "jspace.residency" 469 #define JS_DUAL_SITE_ID_LEN 16u 470 #define JS_DUAL_OBS_KIND 0xD3u /* kind byte: DUAL decision-site observation */ 471 #define JS_DUAL_OBS_VERSION 1u 472 #define JS_DUAL_OBS_DOMAIN "omega.dual.site.jspace_residency.v1" 473 #define JS_DUAL_OBS_N_ALT 4u /* MOVE, COMPRESS, SPILL, EVICT: the chooser's order */ 474 #define JS_DUAL_OBS_BYTES 302u /* canonical encoding length (version 1) */ 475 476 typedef enum { 477 JS_DUAL_PRESSURE_CAPACITY = 1, /* total residency over budget: pressure 1e9, MOVE not allowed */ 478 JS_DUAL_PRESSURE_SOFT = 2 /* only the hot arena over half the budget: pressure 1e3 */ 479 } JsDualPressureCase; 480 481 typedef enum { 482 JS_DUAL_ALT_INADMISSIBLE = 0, /* not built: MOVE without allow_move or not HOT; COMPRESS of a 483 compressed realization or one predicted not to shrink */ 484 JS_DUAL_ALT_UNPRICED = 1, /* built but frees no bytes: skipped by the chooser */ 485 JS_DUAL_ALT_PRICED = 2 /* scored: (now + expected_reads * later) / freed */ 486 } JsDualAltStatus; 487 488 typedef struct { 489 uint8_t action; /* JsAction */ 490 uint8_t status; /* JsDualAltStatus */ 491 double now_ns, later_ns, freed_bytes; /* chooser inputs; 0 when inadmissible */ 492 double score; /* nanoseconds per byte given back; 0 unless PRICED */ 493 } JsDualAlternative; 494 495 struct JsDualObservation_ { 496 uint8_t kind, version; 497 uint64_t seq; /* monotonically increasing per space */ 498 double pressure; /* the value the chooser used */ 499 uint8_t pressure_case; /* JsDualPressureCase */ 500 uint8_t allow_move; 501 uint8_t chosen; /* JsAction; JS_ACT_COUNT = keep */ 502 uint8_t n_priced; 503 JsDualAlternative alt[JS_DUAL_OBS_N_ALT]; 504 double best_score; /* cheapest priced score; 0 when none was priced */ 505 double keep_threshold; /* pressure * retain_ns_per_byte: keep when best >= this */ 506 double expected_reads; 507 /* realization identity and state at the instant of the decision */ 508 uint32_t real_slot; 509 uint64_t real_gen; 510 uint8_t real_type, placement, recipe; 511 uint64_t unit_bytes, held_bytes; 512 uint32_t holders, refs; 513 uint64_t last_use; 514 uint8_t semantic_state_id[32]; 515 /* the instant */ 516 uint64_t budget_bytes, resident_bytes, hot_bytes; 517 uint8_t digest[32]; 518 }; 519 520 /* NULL fn restores the default (no observation). */ 521 void js_dual_set_observer(JsSpace *s, JsDualObserver fn, const void *ctx); 522 /* Canonical bytes of obs (all fields but digest). Returns JS_DUAL_OBS_BYTES. */ 523 size_t js_dual_observation_encode(const JsDualObservation *obs, uint8_t out[JS_DUAL_OBS_BYTES]); 524 525 const char *js_action_name(JsAction a); 526 const char *js_placement_name(JsPlacement p); 527 const char *js_real_type_name(JsRealType t); 528 529 #endif /* RX_JSPACE_H */