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

139 lines · 6017 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 * visor_world.c -- types, formatters and the unattached view (lane 6).
19 * Physics-free and runtime-free: no rx_* symbol and no runtime header here, so
20 * this file links into the V1 `omega` binary. See visor_world.h.
21 */
22#include "visor_world.h"
23
24#include <inttypes.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <string.h>
28
29void visor_world_gap_append(VisorWorldView *v, const char *note) {
30 if (!v || !note || !note[0]) return;
31 size_t cap = sizeof(v->gap_note);
32 size_t len = strnlen(v->gap_note, cap);
33 if (len >= cap - 1) return;
34 int w = snprintf(v->gap_note + len, cap - len, "%s%s", len ? "; " : "", note);
35 (void)w; /* snprintf truncates and NUL-terminates; a full note stays bounded */
36}
37
38void visor_world_unattached(VisorWorldView *out) {
39 if (!out) return;
40 memset(out, 0, sizeof(*out));
41 out->attached = false;
42 out->state_name = "unattached";
43 out->staged_effects_visible = false;
44 visor_world_gap_append(out,
45 "no resident World attached in this session: the V1 omega binary does not "
46 "link the runtime (src/runtime); nothing here is observed");
47}
48
49/* ---- bounded writer --------------------------------------------------- */
50
51typedef struct { char *p; size_t n; size_t len; bool overflow; } Buf;
52
53static void bput(Buf *b, const char *fmt, ...) {
54 if (b->overflow) return;
55 va_list ap;
56 va_start(ap, fmt);
57 int w = vsnprintf(b->p + b->len, b->n - b->len, fmt, ap);
58 va_end(ap);
59 if (w < 0 || (size_t)w >= b->n - b->len) { b->overflow = true; return; }
60 b->len += (size_t)w;
61}
62
63/* JSON string (escapes ", \ and control bytes). NULL -> null. */
64static void bjson_str(Buf *b, const char *s) {
65 if (!s) { bput(b, "null"); return; }
66 bput(b, "\"");
67 for (const unsigned char *c = (const unsigned char *)s; *c && !b->overflow; c++) {
68 if (*c == '"' || *c == '\\') bput(b, "\\%c", *c);
69 else if (*c < 0x20) bput(b, "\\u%04x", (unsigned)*c);
70 else bput(b, "%c", *c);
71 }
72 bput(b, "\"");
73}
74
75static int bfinish(Buf *b) {
76 if (b->overflow) { b->p[0] = '\0'; return -1; }
77 return (int)b->len;
78}
79
80static const char *nz(const char *s, const char *dflt) { return (s && s[0]) ? s : dflt; }
81
82int visor_world_format_text(const VisorWorldView *v, char *out, size_t n) {
83 if (!v || !out || n == 0) return -1;
84 out[0] = '\0';
85 Buf b = { out, n, 0, false };
86 bput(&b, "world: %s\n", v->attached ? "attached" : "unattached");
87 bput(&b, " state: %s\n", nz(v->state_name, "-"));
88 if (v->generation) bput(&b, " generation: %" PRIu64 "\n", v->generation);
89 else bput(&b, " generation: (not observable)\n");
90 bput(&b, " resident objects: %zu (shown %zu)\n", v->resident_object_count, v->object_count);
91 bput(&b, " reactions: %zu\n", v->reaction_count);
92 bput(&b, " active work: %zu\n", v->active_work);
93 bput(&b, " published results: %zu%s\n", v->published_results,
94 v->published_results ? "" : " (not observable)");
95 if (v->staged_effects_visible) bput(&b, " staged effects: %zu\n", v->staged_effects);
96 else bput(&b, " staged effects: (not visible)\n");
97 bput(&b, " publication tail: %" PRIu64 "\n", v->publication_tail);
98 bput(&b, " digest: %s\n", nz(v->digest, "-"));
99 size_t shown = v->object_count > VISOR_WORLD_MAX_OBJECTS ? VISOR_WORLD_MAX_OBJECTS
100 : v->object_count;
101 for (size_t i = 0; i < shown; i++)
102 bput(&b, " object %-22s %-10s %s\n", v->objects[i].id,
103 nz(v->objects[i].kind, "-"), nz(v->objects[i].state, "-"));
104 bput(&b, " gap: %s\n", nz(v->gap_note, "none"));
105 bput(&b, " note: view only; this view holds no authority over the World\n");
106 return bfinish(&b);
107}
108
109int visor_world_format_json(const VisorWorldView *v, char *out, size_t n) {
110 if (!v || !out || n == 0) return -1;
111 out[0] = '\0';
112 Buf b = { out, n, 0, false };
113 bput(&b, "{\"attached\":%s,\"state\":", v->attached ? "true" : "false");
114 bjson_str(&b, v->state_name);
115 bput(&b, ",\"generation\":%" PRIu64 ",\"generation_observable\":%s",
116 v->generation, v->generation ? "true" : "false");
117 bput(&b, ",\"resident_object_count\":%zu,\"reaction_count\":%zu,\"active_work\":%zu",
118 v->resident_object_count, v->reaction_count, v->active_work);
119 bput(&b, ",\"published_results\":%zu,\"staged_effects\":%zu,\"staged_effects_visible\":%s",
120 v->published_results, v->staged_effects, v->staged_effects_visible ? "true" : "false");
121 bput(&b, ",\"publication_tail\":%" PRIu64 ",\"digest\":", v->publication_tail);
122 bjson_str(&b, v->digest[0] ? v->digest : NULL);
123 bput(&b, ",\"objects\":[");
124 size_t shown = v->object_count > VISOR_WORLD_MAX_OBJECTS ? VISOR_WORLD_MAX_OBJECTS
125 : v->object_count;
126 for (size_t i = 0; i < shown; i++) {
127 bput(&b, "%s{\"id\":", i ? "," : "");
128 bjson_str(&b, v->objects[i].id);
129 bput(&b, ",\"kind\":");
130 bjson_str(&b, v->objects[i].kind);
131 bput(&b, ",\"state\":");
132 bjson_str(&b, v->objects[i].state);
133 bput(&b, "}");
134 }
135 bput(&b, "],\"gap_note\":");
136 bjson_str(&b, v->gap_note);
137 bput(&b, ",\"authority\":false}");
138 return bfinish(&b);
139}