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 / visor / test_visor_world.c

186 lines · 8475 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 * test_visor_world.c -- lane 6: read-only World view.
19 *
20 * The FIXTURE builds a World exactly as tests/runtime/rx_heartbeat_test.c does
21 * (rx_caproot_start, rx_world_init, rx_world_create, rx_world_retire). Those
22 * calls belong to the test harness, not to the visor. The code under test
23 * (visor_world_snapshot / formatters) calls no mutator.
24 */
25#include "runtime/rx_caproot.h"
26#include "runtime/rx_world.h"
27#include "visor_world.h"
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33static int g_pass, g_total;
34#define CHECK(cond, msg) do { g_total++; if (cond) g_pass++; \
35 else fprintf(stderr, "FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); } while (0)
36
37typedef struct { RxCapRoot root; RxCapAdmin admin; RxWorld w; } Env;
38
39static int env_start(Env *e) {
40 if (rx_caproot_start(&e->root, &e->admin) != RX_CAP_OK) return -1;
41 if (rx_world_init(&e->w, &e->root, 2, 1u << 12) != RX_OK) {
42 rx_caproot_stop(&e->root, &e->admin);
43 return -1;
44 }
45 e->w.external_subject = 100;
46 return 0;
47}
48
49static void env_stop(Env *e) {
50 rx_world_destroy(&e->w);
51 rx_caproot_stop(&e->root, &e->admin);
52}
53
54static RxObjRef mkobj(Env *e, RxPersist p, uint64_t resource, uint64_t v0) {
55 uint64_t init[RX_MAX_FIELDS] = { v0 };
56 RxObjRef r = { UINT32_MAX, 0 };
57 rx_world_create(&e->w, 1, p, resource, init, &r);
58 return r;
59}
60
61static char t1[8192], t2[8192], j1[8192];
62
63int main(void) {
64 VisorWorldView v, v2;
65
66 /* 1. NULL world fails closed. */
67 memset(&v, 0x5a, sizeof(v));
68 CHECK(visor_world_snapshot(NULL, &v) == -1, "NULL world accepted");
69 CHECK(!v.attached && v.object_count == 0 && v.resident_object_count == 0,
70 "NULL world left a claimed view");
71 CHECK(strstr(v.gap_note, "fail closed") != NULL, "NULL world gap note missing");
72 CHECK(visor_world_snapshot(NULL, NULL) == -1, "NULL out accepted");
73 CHECK(visor_world_format_text(NULL, t1, sizeof t1) == -1, "format NULL view accepted");
74
75 /* 2. Unattached view formats, deterministically, claiming nothing. */
76 visor_world_unattached(&v);
77 int a = visor_world_format_text(&v, t1, sizeof t1);
78 int b = visor_world_format_text(&v, t2, sizeof t2);
79 CHECK(a > 0 && a == b && strcmp(t1, t2) == 0, "unattached text not deterministic");
80 CHECK(strstr(t1, "world: unattached") && strstr(t1, "does not link the runtime"),
81 "unattached text does not say why");
82 CHECK(visor_world_format_json(&v, j1, sizeof j1) > 0 &&
83 strstr(j1, "\"attached\":false") && strstr(j1, "\"authority\":false"),
84 "unattached json wrong");
85 CHECK(!v.attached && !v.staged_effects_visible && v.generation == 0,
86 "unattached view claims state");
87
88 /* 3. A real World, built like rx_heartbeat_test. */
89 Env e;
90 memset(&e, 0, sizeof(e));
91 if (env_start(&e) != 0) {
92 fprintf(stderr, "FAIL: could not start capability root / world\n");
93 printf("FAIL %d/%d\n", g_pass, g_total + 1);
94 return 1;
95 }
96 RxObjRef o1 = mkobj(&e, RX_PERSIST_RESIDENT, 0x10, 7);
97 RxObjRef o2 = mkobj(&e, RX_PERSIST_EPHEMERAL, 0x20, 8);
98 RxObjRef o3 = mkobj(&e, RX_PERSIST_DURABLE, 0x30, 9);
99 CHECK(o1.id != UINT32_MAX && o2.id != UINT32_MAX && o3.id != UINT32_MAX,
100 "fixture could not create objects");
101 CHECK(rx_world_wait_quiescent(&e.w, 2000) == RX_OK, "world not quiescent");
102
103 uint64_t crumbs_before = e.w.n_crumbs;
104 uint8_t d_before[32];
105 rx_world_digest(&e.w, d_before);
106
107 CHECK(visor_world_snapshot(&e.w, &v) == 0, "snapshot failed");
108 CHECK(visor_world_snapshot(&e.w, &v2) == 0, "second snapshot failed");
109 a = visor_world_format_text(&v, t1, sizeof t1);
110 b = visor_world_format_text(&v2, t2, sizeof t2);
111 CHECK(a > 0 && a == b && strcmp(t1, t2) == 0, "attached text not deterministic");
112 a = visor_world_format_text(&v, t2, sizeof t2);
113 CHECK(strcmp(t1, t2) == 0, "same view formats differently twice");
114
115 CHECK(v.attached && v.object_count == 3 && v.resident_object_count == 3,
116 "object counts wrong");
117 CHECK(v.reaction_count == 0 && v.active_work == 0, "reaction/work counts wrong");
118
119 /* 4. Each object matches rx_world_read (generation and persistence). */
120 const RxPersist want[3] = { RX_PERSIST_RESIDENT, RX_PERSIST_EPHEMERAL, RX_PERSIST_DURABLE };
121 const char *want_kind[3] = { "RESIDENT", "EPHEMERAL", "DURABLE" };
122 for (size_t i = 0; i < v.object_count && i < 3; i++) {
123 unsigned id = 0, gen = 0;
124 CHECK(sscanf(v.objects[i].id, "rx:%u@%u", &id, &gen) == 2, "object id unparsable");
125 RxObject ro;
126 RxObjRef ref = { id, gen };
127 CHECK(rx_world_read(&e.w, ref, &ro) == RX_OK, "view names an object rx_world_read rejects");
128 CHECK(ro.generation == gen && ro.id == id, "generation differs from rx_world_read");
129 CHECK(ro.persist == want[i] && strcmp(v.objects[i].kind, want_kind[i]) == 0,
130 "kind differs from runtime persistence");
131 CHECK(strcmp(v.objects[i].state, ro.placed ? "live,placed" : "live") == 0,
132 "state differs from runtime placement");
133 }
134
135 /* 5. Digest and tail match the public read calls. */
136 char dhex[72];
137 memcpy(dhex, "sha256:", 7);
138 for (int i = 0; i < 32; i++) snprintf(dhex + 7 + 2 * i, 3, "%02x", d_before[i]);
139 CHECK(strcmp(v.digest, dhex) == 0, "digest differs from rx_world_digest");
140 CHECK(v.publication_tail == rx_world_publication_tail(&e.w), "tail differs");
141
142 /* 6. No field claims visibility the runtime does not grant. */
143 CHECK(!v.staged_effects_visible && v.staged_effects == 0, "staged effects claimed");
144 CHECK(v.generation == 0 && strstr(v.gap_note, "generation"), "generation claimed");
145 CHECK(v.published_results == 0 && strstr(v.gap_note, "published results"),
146 "published results claimed");
147 CHECK(strstr(t1, "(not visible)") && strstr(t1, "generation: (not observable)"),
148 "text claims unobservable fields");
149 CHECK(visor_world_format_json(&v, j1, sizeof j1) > 0 &&
150 strstr(j1, "\"staged_effects_visible\":false") &&
151 strstr(j1, "\"generation_observable\":false") && strstr(j1, "\"authority\":false"),
152 "json claims visibility");
153
154 /* 7. Snapshot mutated nothing. */
155 uint8_t d_after[32];
156 rx_world_digest(&e.w, d_after);
157 CHECK(memcmp(d_before, d_after, 32) == 0 && e.w.n_crumbs == crumbs_before,
158 "snapshot changed the World");
159
160 /* 8. A retired object disappears; its stale ref is refused by the runtime. */
161 CHECK(rx_world_retire(&e.w, o2) == RX_OK, "fixture retire failed");
162 CHECK(visor_world_snapshot(&e.w, &v) == 0 && v.object_count == 2, "retired object shown");
163 char stale[72];
164 snprintf(stale, sizeof stale, "rx:%u@%u", o2.id, o2.generation);
165 int seen = 0;
166 for (size_t i = 0; i < v.object_count; i++) seen |= strcmp(v.objects[i].id, stale) == 0;
167 CHECK(!seen, "stale generation still listed");
168
169 /* 9. Bounded: more than 32 live objects truncates honestly. */
170 for (int i = 0; i < 40; i++) mkobj(&e, RX_PERSIST_EPHEMERAL, 0x40, (uint64_t)i);
171 CHECK(visor_world_snapshot(&e.w, &v) == 0 && v.object_count == 32 &&
172 v.resident_object_count == 42 && strstr(v.gap_note, "truncated"),
173 "truncation not bounded/declared");
174 CHECK(strstr(v.gap_note, "quiescent") != NULL, "gap note itself was cut off");
175
176 /* 10. Output that does not fit fails closed. */
177 char tiny[16];
178 CHECK(visor_world_format_text(&v, tiny, sizeof tiny) == -1 && tiny[0] == '\0',
179 "text overflow not refused");
180 CHECK(visor_world_format_json(&v, tiny, sizeof tiny) == -1 && tiny[0] == '\0',
181 "json overflow not refused");
182
183 env_stop(&e);
184 printf("%s %d/%d\n", g_pass == g_total ? "PASS" : "FAIL", g_pass, g_total);
185 return g_pass == g_total ? 0 : 1;
186}