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
omega-mv / src / visor / rx / visor_world_rx.c
| 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 | * visor_world_rx.c -- visor_world_snapshot(): the only visor code that touches |
| 19 | * an RxWorld (lane 6). Lives in src/visor/rx/ so the top-level src/visor wildcard does |
| 20 | * NOT link it into the physics-free, runtime-free V1 `omega` binary. Built only |
| 21 | * by mk/visor-world.mk. |
| 22 | * |
| 23 | * Read-only. Calls no runtime mutator and no authority-bearing function. Takes |
| 24 | * the world mutex exactly once (the same lock rx_world_read takes) to copy a |
| 25 | * few public scalars, releases it, then calls the public read functions, which |
| 26 | * each lock on their own. Never holds the mutex across an rx_* call (the |
| 27 | * mutex is not recursive). Never waits for quiescence. |
| 28 | */ |
| 29 | #include "visor_world.h" |
| 30 | #include "runtime/rx_world.h" |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | static const char *persist_name(RxPersist p) { |
| 36 | switch (p) { |
| 37 | case RX_PERSIST_EPHEMERAL: return "EPHEMERAL"; |
| 38 | case RX_PERSIST_RESIDENT: return "RESIDENT"; |
| 39 | case RX_PERSIST_DURABLE: return "DURABLE"; |
| 40 | default: return "?"; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | typedef struct { uint32_t id, generation; RxPersist persist; bool placed; } ObjCopy; |
| 45 | |
| 46 | int visor_world_snapshot(const struct RxWorld *cw, VisorWorldView *out) { |
| 47 | if (!out) return -1; |
| 48 | if (!cw) { |
| 49 | memset(out, 0, sizeof(*out)); |
| 50 | out->state_name = "unattached"; |
| 51 | visor_world_gap_append(out, "no World given: snapshot refused (fail closed)"); |
| 52 | return -1; |
| 53 | } |
| 54 | /* The runtime's read API is not const-qualified and locking needs a |
| 55 | * mutable mutex. The visor changes no World state through this pointer. */ |
| 56 | RxWorld *w = (RxWorld *)cw; |
| 57 | |
| 58 | memset(out, 0, sizeof(*out)); |
| 59 | out->attached = true; |
| 60 | out->state_name = "resident"; /* fixed label: the World has no lifecycle state */ |
| 61 | |
| 62 | ObjCopy objs[VISOR_WORLD_MAX_OBJECTS]; |
| 63 | size_t n_live = 0, n_copied = 0; |
| 64 | uint32_t n_reactions, in_flight; |
| 65 | |
| 66 | pthread_mutex_lock(&w->mu); |
| 67 | for (uint32_t i = 0; i < RX_MAX_OBJECTS; i++) { |
| 68 | const RxObject *o = &w->objects[i]; |
| 69 | if (!o->live) continue; |
| 70 | if (n_copied < VISOR_WORLD_MAX_OBJECTS) { |
| 71 | objs[n_copied].id = o->id; |
| 72 | objs[n_copied].generation = o->generation; |
| 73 | objs[n_copied].persist = o->persist; |
| 74 | objs[n_copied].placed = o->placed; |
| 75 | n_copied++; |
| 76 | } |
| 77 | n_live++; |
| 78 | } |
| 79 | n_reactions = w->n_reactions; |
| 80 | in_flight = w->in_flight; |
| 81 | pthread_mutex_unlock(&w->mu); |
| 82 | |
| 83 | out->resident_object_count = n_live; |
| 84 | out->reaction_count = n_reactions; |
| 85 | out->active_work = in_flight; |
| 86 | for (size_t i = 0; i < n_copied; i++) { |
| 87 | snprintf(out->objects[i].id, sizeof(out->objects[i].id), "rx:%u@%u", |
| 88 | objs[i].id, objs[i].generation); |
| 89 | out->objects[i].kind = persist_name(objs[i].persist); |
| 90 | out->objects[i].state = objs[i].placed ? "live,placed" : "live"; |
| 91 | } |
| 92 | out->object_count = n_copied; |
| 93 | |
| 94 | /* Public read calls, each under its own lock acquisition. */ |
| 95 | uint8_t d[32]; |
| 96 | rx_world_digest(w, d); |
| 97 | memcpy(out->digest, "sha256:", 7); |
| 98 | for (int i = 0; i < 32; i++) |
| 99 | snprintf(out->digest + 7 + 2 * i, 3, "%02x", d[i]); |
| 100 | out->publication_tail = rx_world_publication_tail(w); |
| 101 | |
| 102 | /* What the runtime cannot show through a coherent read today. */ |
| 103 | out->generation = 0; |
| 104 | out->published_results = 0; |
| 105 | out->staged_effects = 0; |
| 106 | out->staged_effects_visible = false; |
| 107 | if (n_live > n_copied) |
| 108 | visor_world_gap_append(out, "object list truncated at 32"); |
| 109 | visor_world_gap_append(out, "generation: RxWorld holds no RxGenStore"); |
| 110 | visor_world_gap_append(out, "published results: kept per RcGate, not on RxWorld"); |
| 111 | visor_world_gap_append(out, "staged effects: RxGenStore only, not visible"); |
| 112 | visor_world_gap_append(out, "digest/tail read apart from objects, coherent only when quiescent"); |
| 113 | return 0; |
| 114 | } |