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 / turing / ty_qrecord.h

199 lines · 9611 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/* TURING companion records for continuous-value (quantized) evidence, and the
18 * sidecar manifest line formats they bind. New digest domains only; no
19 * existing turing.* record, domain or golden is touched.
20 *
21 * turing.qprofile.v0 measurement profile (constraint)
22 * turing.qworld.v0 one scored world block (evidence)
23 * turing.qfreeze.v0 frozen artifact and its pre-run checks (evidence)
24 * turing.qgain.v0 one T measurement (evidence)
25 *
26 * Digest = SHA-256(domain || 0x00 || OMG0 bytes) via omega_canonical_encode
27 * and src/sha256.c, exactly as ty_record.c. These are record digests, never
28 * Omega semantic ids (OSC-0B).
29 *
30 * The library carries every descriptive value (names, versions, rule ids,
31 * counts) as caller-supplied data; it holds no profile constants. Values
32 * longer than 512 bytes and keys of 64 bytes or more are refused with
33 * TY_E_FORMAT, never truncated. Builders and verifiers refuse a malformed
34 * record; a verifier additionally recomputes the digest.
35 *
36 * Return codes are TY_E_* from ty_math.h (TY_OK on success), plus
37 * TYQR_E_EXISTS for a write-once writer that finds the file already there.
38 */
39#ifndef TURING_TY_QRECORD_H
40#define TURING_TY_QRECORD_H
41
42#include <stddef.h>
43#include <stdint.h>
44
45#include "turing/ty_record.h"
46
47#define TYQR_DOMAIN_PROFILE "turing.qprofile.v0"
48#define TYQR_DOMAIN_WORLD "turing.qworld.v0"
49#define TYQR_DOMAIN_FREEZE "turing.qfreeze.v0"
50#define TYQR_DOMAIN_GAIN "turing.qgain.v0"
51
52#define TYQR_E_EXISTS (-201)
53#define TYQR_VAL 513 /* 512 value bytes plus NUL */
54#define TYQR_MAX_LINEAGE 8
55#define TYQR_CANON_CAP 40000
56
57/* Text fields are printable ASCII (0x20..0x7e), non-empty. List fields are
58 * tokens separated by exactly one space (no leading, trailing or doubled
59 * space). Digest fields: an all-zero digest is the "absent" value. */
60
61typedef struct {
62 char profile[TYQR_VAL];
63 ty_digest spec, packet;
64 char protocol[TYQR_VAL], quantizer[TYQR_VAL];
65 int64_t delta_log2;
66 char edge_rule[TYQR_VAL];
67 int64_t sd_min_log2;
68 char floor_rule[TYQR_VAL];
69 char families[TYQR_VAL]; /* list */
70 uint64_t kmax, n_points, split;
71 ty_digest cells; /* digest of the cell-table sidecar */
72 char rider[TYQR_VAL], lm_rule[TYQR_VAL];
73 char baselines[TYQR_VAL]; /* list */
74 ty_digest thresholds; /* digest of the thresholds sidecar */
75 char thresholds_format[TYQR_VAL], gen_version[TYQR_VAL];
76 uint64_t bootstrap_b;
77 char unit[TYQR_VAL];
78} tyqr_profile;
79
80typedef struct {
81 ty_digest qprofile;
82 char mode[TYQR_VAL]; /* dev | certification | discovery */
83 char parent_kind[TYQR_VAL]; /* dev | sealed */
84 ty_digest qfreeze; /* zero iff parent_kind = dev */
85 char cell[TYQR_VAL];
86 uint64_t block;
87 char label[TYQR_VAL];
88 char gen_version[TYQR_VAL];
89 ty_digest generator;
90 uint64_t rep_first, rep_count;
91 ty_digest traj_manifest;
92 char traj_format[TYQR_VAL];
93} tyqr_world;
94
95typedef struct {
96 char mode[TYQR_VAL]; /* certification | discovery */
97 ty_digest artifact;
98 char artifact_kind[TYQR_VAL];
99 ty_digest artifact_bin; /* zero for certification */
100 ty_digest qprofile, dev_manifest;
101 char b2_emerging_label[TYQR_VAL]; /* POSITIVE | NEGATIVE | AMBIGUOUS */
102 char bayes_plugin_check[TYQR_VAL];/* AGREE | STOP */
103 char diag_fw_rate_ppm[TYQR_VAL]; /* list */
104 char lengthening_test[TYQR_VAL]; /* PASS | FAIL */
105 char stability_test[TYQR_VAL]; /* PASS | FAIL */
106 char calibration_extra[TYQR_VAL];
107 char run_commit[41]; /* 40 lowercase hex */
108 uint64_t tree_dirty; /* 0 or 1 */
109 char base_commits[TYQR_VAL]; /* list */
110 char producer_tool[TYQR_VAL], producer_model[TYQR_VAL];
111 ty_digest prompt_digest, transcript_digest;
112 uint64_t n_lineage; /* 0..8 */
113 ty_digest lineage[TYQR_MAX_LINEAGE];
114} tyqr_freeze;
115
116typedef struct {
117 ty_digest qprofile, qworld, qfreeze;
118 char mode[TYQR_VAL]; /* certification | discovery */
119 char cell[TYQR_VAL], gen_version[TYQR_VAL];
120 uint64_t baseline_opcode; /* 1..7 */
121 ty_digest baseline_artifact;
122 ty_digest candidate;
123 uint64_t candidate_opcode; /* 1..7, or 255 */
124 ty_digest prd_b, prd_m;
125 uint64_t official; /* 1 or 0 */
126 char ladder_item[TYQR_VAL]; /* "-" iff official */
127 int64_t lengths[6]; /* L(B) L(D|B) L(M) L(D|M) DL(B,D) DL(M,D) */
128 int64_t t_ub, t_lo_ub, t_hi_ub, numeric_bound_ub, mc_err_ub;
129 uint64_t counts[4]; /* B worlds heldout_per_world scored_points */
130 uint64_t floor_hits, protocol_failures;
131 char leak_flag[TYQR_VAL]; /* 0 | 1 | INCONCLUSIVE */
132 int64_t coverage[3]; /* -1 where absent */
133 char diag_warnings[TYQR_VAL]; /* list or "-" */
134 char classification[TYQR_VAL]; /* POSITIVE NEGATIVE AMBIGUOUS WEAK STOPPED */
135 char status[TYQR_VAL]; /* PASS | FAIL_* | STOPPED */
136 char unit[TYQR_VAL];
137} tyqr_gain;
138
139/* Digest of a record; refuses (TY_E_FORMAT / TY_E_ARG) anything malformed. */
140int tyqr_profile_digest(const tyqr_profile *r, ty_digest *out);
141int tyqr_world_digest(const tyqr_world *r, ty_digest *out);
142int tyqr_freeze_digest(const tyqr_freeze *r, ty_digest *out);
143int tyqr_gain_digest(const tyqr_gain *r, ty_digest *out);
144
145/* Rebuild from the fields and compare with the recorded digest:
146 * TY_OK, TY_E_DIGEST on mismatch, TY_E_FORMAT on a malformed record. */
147int tyqr_profile_verify(const tyqr_profile *r, const ty_digest *want, char *why, size_t whylen);
148int tyqr_world_verify(const tyqr_world *r, const ty_digest *want, char *why, size_t whylen);
149int tyqr_freeze_verify(const tyqr_freeze *r, const ty_digest *want, char *why, size_t whylen);
150int tyqr_gain_verify(const tyqr_gain *r, const ty_digest *want, char *why, size_t whylen);
151
152/* Cross-record binding for a gain (TY_E_PROFILE on any disagreement):
153 * the gain, world and freeze all name the same qprofile digest (P); the gain
154 * names the world's digest and the freeze's digest; mode, cell and
155 * gen_version equal the world's; the world is sealed by that freeze; the
156 * freeze's mode equals the gain's; candidate equals the freeze's artifact.
157 * Each record is also verified against its digest. */
158int tyqr_gain_bind(const tyqr_gain *g, const ty_digest *g_digest,
159 const tyqr_world *w, const ty_digest *w_digest,
160 const tyqr_freeze *f, const ty_digest *f_digest,
161 const ty_digest *profile_digest, char *why, size_t whylen);
162
163/* Write-once, digest-named writer: <dir>/<record>.<digest hex>.omg0 holding
164 * the canonical OMG0 bytes, O_CREAT|O_EXCL, mode 0444. Returns TY_OK,
165 * TYQR_E_EXISTS if the file is already there (left untouched), or an error.
166 * `path` (may be NULL) receives the file name. */
167int tyqr_profile_write(const char *dir, const tyqr_profile *r, ty_digest *d, char *path, size_t pathlen);
168int tyqr_world_write(const char *dir, const tyqr_world *r, ty_digest *d, char *path, size_t pathlen);
169int tyqr_freeze_write(const char *dir, const tyqr_freeze *r, ty_digest *d, char *path, size_t pathlen);
170int tyqr_gain_write(const char *dir, const tyqr_gain *r, ty_digest *d, char *path, size_t pathlen);
171
172/* Recompute SHA-256(domain || 0x00 || file bytes) of a stored record file
173 * and compare with `want` (TY_E_DIGEST on mismatch, TY_E_IO if unreadable). */
174int tyqr_file_verify(const char *path, const char *domain, const ty_digest *want);
175
176/* ---- sidecar manifests: ASCII, one record per line, fields separated by one
177 * space, every line ends with LF (including the last), no other bytes. The
178 * sidecar digest is SHA-256 of the file bytes. ---- */
179#define TYQR_CELL_MAX 128
180
181typedef struct { uint64_t rep; ty_digest d; } tyqr_repline; /* traj, prd */
182typedef struct { char cell[TYQR_CELL_MAX]; uint64_t block; ty_digest d; } tyqr_devline;
183
184/* Render `<rep dec> <digest hex>` lines, rep strictly ascending. */
185int tyqr_repman_build(const tyqr_repline *e, size_t n, char *out, size_t cap, size_t *len);
186int tyqr_repman_parse(const char *buf, size_t len, tyqr_repline *out, size_t cap, size_t *n);
187/* Render `<cell> <block dec> <digest hex>`: cell by byte order, then block
188 * ascending (strictly). */
189int tyqr_devman_build(const tyqr_devline *e, size_t n, char *out, size_t cap, size_t *len);
190int tyqr_devman_parse(const char *buf, size_t len, tyqr_devline *out, size_t cap, size_t *n);
191/* Format checks only (the values are caller-supplied):
192 * cell table: `<cell> <field>=<value> ...`, cells strictly ascending bytes
193 * thresholds: `<item_id> <field> <value>`, lines strictly ascending bytes */
194int tyqr_celltab_check(const char *buf, size_t len);
195int tyqr_thrtab_check(const char *buf, size_t len);
196/* SHA-256 of sidecar bytes. */
197void tyqr_sidecar_digest(const char *buf, size_t len, ty_digest *out);
198
199#endif