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 / algebra / ma_digital_v1_gate.c

518 lines · 23203 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/* MIXED_ALGEBRA_DIGITAL_V1 gate driver (spec/mixed-algebra-digital-v1.md).
18 *
19 * Runs the pre-registered selection queries through the EXISTING TURING Field
20 * selector v1 using only public turing_* functions, checks the gate criteria
21 * that live in the decisions, and writes the wrapper receipt JSON.
22 * No timed runs here; timing comes from the stored bench receipts.
23 *
24 * ma_digital_v1_gate digests FRESH1 FRESH2
25 * one line per query: "<id> <decision digest hex>" (reproduction)
26 * ma_digital_v1_gate receipt OUT.json FRESH1 FRESH2 CONT1 CONT2
27 * full gate over the stored receipts; environment (set by
28 * ma_digital_v1_gate.sh): DV1_COMMIT, DV1_TREE_DIRTY (0|1), DV1_MODE
29 * (fresh|reuse), DV1_RUN_ID, DV1_CORRECTNESS (file of summary lines,
30 * one "PASS|FAIL <label>: <line>" per command), DV1_REPRO_FILES
31 * (space-separated digest files from separate processes, "label=path"),
32 * DV1_UNTRACKED (file listing untracked paths at run time);
33 * optional DV1_EXPECT_SHA256 ("path=hex ..."), see check_expected.
34 * Exit: 0 verdict PASS, 3 verdict FAIL (receipt still written), 2 error.
35 */
36#include "algebra/realize_common.h"
37#include "turing/select.h"
38
39#include <inttypes.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44typedef struct {
45 const char *id, *what;
46 uint64_t n, m;
47 int pack;
48 int expect_exact;
49 int above_max_n; /* named-rejection query */
50} dv1_query;
51
52/* Pre-registered, spec section 5. Sparsity 0.30 for all. */
53static const dv1_query Q[] = {
54 {"Q1", "L2-resident (256 KiB int8 W)", 4096, 64, TURING_PACK_ONCE, 1, 0},
55 {"Q2", "L2-resident (256 KiB int8 W)", 4096, 64, TURING_PACK_PER_CALL, 1, 0},
56 {"Q3", "DRAM-bound (64 MiB int8 W)", 16384, 4096, TURING_PACK_ONCE, 1, 0},
57 {"Q4", "DRAM-bound (64 MiB int8 W)", 16384, 4096, TURING_PACK_PER_CALL, 1, 0},
58 {"Q5", "above RNS and sparse max_n", 65537, 64, TURING_PACK_ONCE, 0, 1},
59 {"Q6", "above RNS and sparse max_n", 65537, 64, TURING_PACK_PER_CALL, 0, 1},
60};
61#define NQ (sizeof Q / sizeof Q[0])
62#define SPARSITY_MILLI 300u
63
64static const char *pack_name(int p) { return p == TURING_PACK_PER_CALL ? "per_call" : "once"; }
65
66static int load(turing_store *st, const char *a, const char *b) {
67 if (turing_ingest_registry(st) < 0) return -1;
68 if (turing_ingest_receipt(st, a) <= 0) { fprintf(stderr, "cannot ingest %s\n", a); return -1; }
69 if (turing_ingest_receipt(st, b) <= 0) { fprintf(stderr, "cannot ingest %s\n", b); return -1; }
70 return 0;
71}
72
73static int select_q(const turing_store *st, size_t i, turing_decision *d, turing_digest *id) {
74 turing_query q = {Q[i].n, Q[i].m, SPARSITY_MILLI, Q[i].pack, 0};
75 int rc = turing_field_select(st, &q, NULL, d);
76 if (rc < 0) return -1;
77 if (turing_decision_digest(d, id) != 0) return -1;
78 return rc;
79}
80
81static int is_eligible(turing_reason r) { return r == TURING_R_CHOSEN || r == TURING_R_RANKED || r == TURING_R_TIED; }
82
83static const char *family_of(const char *rz) {
84 const oma_rz_impl *im = oma_rz_find(rz);
85 return im ? im->family : "?";
86}
87
88static int fam_class(const char *f) { /* 1 = R1 binary, 2 = R2 packed ternary, 3 = R3 residue, 0 other */
89 if (!strcmp(f, "binary")) return 1;
90 if (!strcmp(f, "bitplane") || !strcmp(f, "lut") || !strcmp(f, "crumb2") || !strcmp(f, "dense5")) return 2;
91 if (!strcmp(f, "rns")) return 3;
92 return 0;
93}
94
95/* ------------------------------------------------------------ small IO */
96
97static char *slurp(const char *path) {
98 FILE *f = fopen(path, "rb");
99 if (!f) return NULL;
100 size_t cap = 1 << 16, len = 0;
101 char *buf = malloc(cap);
102 if (!buf) { fclose(f); return NULL; }
103 size_t r;
104 while ((r = fread(buf + len, 1, cap - len - 1, f)) > 0) {
105 len += r;
106 if (cap - len < 2) {
107 char *nb = realloc(buf, cap * 2);
108 if (!nb) { free(buf); fclose(f); return NULL; }
109 buf = nb;
110 cap *= 2;
111 }
112 }
113 fclose(f);
114 buf[len] = 0;
115 return buf;
116}
117
118/* "key": "value" -> value; "key": true/false/number -> token. */
119static int jget(const char *text, const char *key, char *out, size_t cap) {
120 char pat[80];
121 snprintf(pat, sizeof pat, "\"%s\": ", key);
122 const char *p = strstr(text, pat);
123 if (!p) return -1;
124 p += strlen(pat);
125 size_t k = 0;
126 if (*p == '"') {
127 ++p;
128 while (*p && *p != '"' && k + 1 < cap) out[k++] = *p++;
129 } else {
130 while (*p && *p != ',' && *p != '}' && *p != '\n' && k + 1 < cap) out[k++] = *p++;
131 }
132 out[k] = 0;
133 return 0;
134}
135
136static void jstr(FILE *o, const char *s) {
137 fputc('"', o);
138 for (; *s; ++s) {
139 unsigned char c = (unsigned char)*s;
140 if (c == '"' || c == '\\') fprintf(o, "\\%c", c);
141 else if (c < 0x20) fprintf(o, "\\u%04x", c);
142 else fputc(c, o);
143 }
144 fputc('"', o);
145}
146
147typedef struct {
148 const char *path, *role;
149 char hex[65], schema[96], run_id[128], run_commit[48], tree_dirty[8], mismatches[24];
150} rinfo;
151
152static int receipt_info(rinfo *r) {
153 turing_digest d;
154 if (turing_file_digest(r->path, &d) != 0) return -1;
155 turing_hex(&d, r->hex);
156 char *t = slurp(r->path);
157 if (!t) return -1;
158 strcpy(r->schema, "?"), strcpy(r->run_id, "?"), strcpy(r->run_commit, "?");
159 strcpy(r->tree_dirty, "?"), strcpy(r->mismatches, "?");
160 (void)jget(t, "schema", r->schema, sizeof r->schema);
161 (void)jget(t, "run_id", r->run_id, sizeof r->run_id);
162 (void)jget(t, "run_commit", r->run_commit, sizeof r->run_commit);
163 (void)jget(t, "tree_dirty", r->tree_dirty, sizeof r->tree_dirty);
164 const char *c = strstr(t, "\"correctness\": {");
165 if (c) (void)jget(c, "mismatches", r->mismatches, sizeof r->mismatches);
166 free(t);
167 return 0;
168}
169
170/* ------------------------------------------------------------- digests */
171
172static int print_digests(FILE *o, const turing_store *st) {
173 for (size_t i = 0; i < NQ; ++i) {
174 turing_decision d;
175 turing_digest id;
176 char h[65];
177 if (select_q(st, i, &d, &id) < 0) return -1;
178 turing_hex(&id, h);
179 fprintf(o, "%s %s\n", Q[i].id, h);
180 }
181 return 0;
182}
183
184/* Mean per-call cost of spec k in footprint (cn, cm) from one receipt. */
185static double receipt_cost(const turing_store *st, const turing_digest *spec, const turing_digest *rd, uint64_t cn,
186 uint64_t cm, int pack) {
187 double s = 0;
188 unsigned c = 0;
189 for (size_t i = 0; i < st->nev; ++i) {
190 const turing_evidence *e = &st->ev[i];
191 if (e->n != cn || e->m != cm || !turing_digest_eq(&e->spec_id, spec) || !turing_digest_eq(&e->receipt_digest, rd))
192 continue;
193 s += (double)turing_ev_cost(e, pack);
194 c++;
195 }
196 return c ? s / c : -1;
197}
198
199static uint64_t weight_bytes_of(const turing_store *st, const turing_digest *spec, uint64_t cn, uint64_t cm) {
200 for (size_t i = 0; i < st->nev; ++i) {
201 const turing_evidence *e = &st->ev[i];
202 if (e->n == cn && e->m == cm && e->sparsity_milli == SPARSITY_MILLI && turing_digest_eq(&e->spec_id, spec))
203 return e->weight_bytes;
204 }
205 return 0;
206}
207
208#define FAILS_MAX 64
209static char g_fails[FAILS_MAX][200];
210static size_t g_nfail;
211static void fail(const char *fmt, const char *a, const char *b) {
212 if (g_nfail < FAILS_MAX) snprintf(g_fails[g_nfail], sizeof g_fails[0], fmt, a, b);
213 g_nfail++;
214}
215
216/* Optional receipt pinning (post-run addendum 2026-09-29). DV1_EXPECT_SHA256
217 * = space-separated "path=<sha256 hex>" pairs, recorded by the gate script
218 * when the bench receipts are written (fresh) or chosen (reuse). Every listed
219 * file must still hash to its recorded digest, and when the variable is set
220 * both fresh receipts must be listed. Checked before ingest and again just
221 * before the wrapper is written. A mismatch is an error (exit 2, no receipt),
222 * not a gate criterion: it means the inputs changed under the gate. */
223static int check_expected(const char *when, const char *f1, const char *f2) {
224 const char *env = getenv("DV1_EXPECT_SHA256");
225 if (!env || !*env) return 0;
226 char list[2048];
227 if (strlen(env) >= sizeof list) { fprintf(stderr, "DV1_EXPECT_SHA256 too long\n"); return -1; }
228 strcpy(list, env);
229 int seen1 = 0, seen2 = 0;
230 for (char *tok = strtok(list, " "); tok; tok = strtok(NULL, " ")) {
231 char *eq = strchr(tok, '=');
232 if (!eq || strlen(eq + 1) != 64) { fprintf(stderr, "DV1_EXPECT_SHA256: bad entry '%s'\n", tok); return -1; }
233 *eq = 0;
234 turing_digest d;
235 char h[65];
236 if (turing_file_digest(tok, &d) != 0) { fprintf(stderr, "%s: cannot hash %s\n", when, tok); return -1; }
237 turing_hex(&d, h);
238 if (strcmp(h, eq + 1)) {
239 fprintf(stderr, "%s: receipt %s changed since it was recorded (sha256 %s, expected %s)\n", when, tok, h, eq + 1);
240 return -1;
241 }
242 seen1 |= !strcmp(tok, f1);
243 seen2 |= !strcmp(tok, f2);
244 }
245 if (!seen1 || !seen2) { fprintf(stderr, "DV1_EXPECT_SHA256 must list both fresh receipts\n"); return -1; }
246 return 0;
247}
248
249static int do_receipt(const char *out, const char *f1, const char *f2, const char *c1, const char *c2) {
250 const char *commit = getenv("DV1_COMMIT"), *dirty = getenv("DV1_TREE_DIRTY"), *mode = getenv("DV1_MODE"),
251 *run_id = getenv("DV1_RUN_ID"), *corr = getenv("DV1_CORRECTNESS"), *repro = getenv("DV1_REPRO_FILES"),
252 *untracked = getenv("DV1_UNTRACKED");
253 if (!commit || !dirty || !mode || !run_id || !corr) {
254 fprintf(stderr, "missing DV1_* environment\n");
255 return 2;
256 }
257 if (check_expected("before ingest", f1, f2)) return 2;
258 int tree_dirty = strcmp(dirty, "0") != 0;
259 turing_store *st = turing_store_new(), *st2 = turing_store_new(), *stc = turing_store_new();
260 if (!st || !st2 || !stc) return 2;
261 if (load(st, f1, f2) || load(st2, f1, f2) || load(stc, c1, c2)) return 2;
262
263 rinfo ri[4] = {{f1, "primary", {0}, {0}, {0}, {0}, {0}, {0}},
264 {f2, "primary", {0}, {0}, {0}, {0}, {0}, {0}},
265 {c1, "continuity", {0}, {0}, {0}, {0}, {0}, {0}},
266 {c2, "continuity", {0}, {0}, {0}, {0}, {0}, {0}}};
267 for (size_t r = 0; r < 4; ++r)
268 if (receipt_info(&ri[r]) != 0) return 2;
269
270 /* criterion 1 (bench part) and 2 (fresh receipts at HEAD, clean) */
271 if (tree_dirty) fail("tree_dirty is true%s%s", "", "");
272 for (size_t r = 0; r < 2; ++r) {
273 if (strcmp(ri[r].mismatches, "0")) fail("fresh receipt %s reports mismatches=%s", ri[r].path, ri[r].mismatches);
274 if (strcmp(ri[r].run_commit, commit)) fail("fresh receipt %s run_commit %s is not HEAD", ri[r].path, ri[r].run_commit);
275 if (strcmp(ri[r].tree_dirty, "false")) fail("fresh receipt %s tree_dirty=%s", ri[r].path, ri[r].tree_dirty);
276 }
277 /* correctness file: every line must start with PASS */
278 char *ctext = slurp(corr);
279 if (!ctext) return 2;
280 for (char *l = ctext; *l;) {
281 char *nl = strchr(l, '\n');
282 if (nl) *nl = 0;
283 if (*l && strncmp(l, "PASS ", 5)) fail("correctness: %s%s", l, "");
284 if (!nl) break;
285 *nl = '\n';
286 l = nl + 1;
287 }
288
289 turing_decision D[NQ], D2[NQ], DC[NQ];
290 turing_digest ID[NQ], ID2[NQ], IDC[NQ];
291 int rc[NQ], verify_ok[NQ], cites_fresh[NQ], repro_in[NQ];
292 char why[NQ][200];
293 size_t nrej = 0;
294 for (size_t i = 0; i < NQ; ++i) {
295 rc[i] = select_q(st, i, &D[i], &ID[i]);
296 int rc2 = select_q(st2, i, &D2[i], &ID2[i]);
297 int rcc = select_q(stc, i, &DC[i], &IDC[i]);
298 if (rc[i] < 0 || rc2 < 0 || rcc < 0) return 2;
299 repro_in[i] = rc[i] == rc2 && turing_digest_eq(&ID[i], &ID2[i]);
300 if (!repro_in[i]) fail("%s: in-process re-selection digest differs%s", Q[i].id, "");
301 if (rc[i] != 0 || D[i].chosen < 0) fail("%s: no realization chosen (verdict %s)", Q[i].id, D[i].verdict);
302 why[i][0] = 0;
303 verify_ok[i] = turing_decision_verify(st, &D[i], why[i], sizeof why[i]) == 0;
304 if (!verify_ok[i]) fail("%s: decision verify failed: %s", Q[i].id, why[i]);
305 cites_fresh[i] = D[i].ncite > 0;
306 for (size_t c = 0; c < D[i].ncite; ++c) {
307 int e = turing_find_evidence(st, &D[i].cite[c]);
308 char h[65] = "";
309 if (e >= 0) turing_hex(&st->ev[e].receipt_digest, h);
310 if (e < 0 || (strcmp(h, ri[0].hex) && strcmp(h, ri[1].hex))) cites_fresh[i] = 0;
311 }
312 if (!cites_fresh[i]) fail("%s: a cited evidence row is not from a fresh receipt%s", Q[i].id, "");
313 if ((D[i].exact_cell != 0) != (Q[i].expect_exact != 0))
314 fail("%s: exact_cell differs from pre-registration (%s)", Q[i].id, D[i].exact_cell ? "exact" : "nearest");
315 /* families */
316 char fams[TURING_MAX_CAND][32];
317 size_t nf = 0, nel = 0;
318 int cls[4] = {0};
319 for (size_t k = 0; k < D[i].ncand; ++k) {
320 if (D[i].reason[k] == TURING_R_MAX_N) nrej++;
321 if (!is_eligible(D[i].reason[k])) {
322 int expected = Q[i].above_max_n && D[i].reason[k] == TURING_R_MAX_N &&
323 (!strcmp(D[i].cand_rz[k], "R3_sparse") || !strcmp(D[i].cand_rz[k], "R4_rns"));
324 if (!expected)
325 fail("%s: unexpected filter on %s", Q[i].id, D[i].cand_rz[k]);
326 continue;
327 }
328 nel++;
329 const char *f = family_of(D[i].cand_rz[k]);
330 cls[fam_class(f)] = 1;
331 size_t j = 0;
332 while (j < nf && strcmp(fams[j], f)) ++j;
333 if (j == nf) snprintf(fams[nf++], sizeof fams[0], "%s", f);
334 }
335 if (nf < 3) fail("%s: verified-eligible set spans fewer than 3 families%s", Q[i].id, "");
336 if (!Q[i].above_max_n && (nel != oma_rz_count() || !cls[1] || !cls[2] || !cls[3]))
337 fail("%s: not all realizations / classes R1,R2,R3 verified-eligible%s", Q[i].id, "");
338 if (Q[i].above_max_n) {
339 size_t mx = 0;
340 for (size_t k = 0; k < D[i].ncand; ++k) mx += D[i].reason[k] == TURING_R_MAX_N;
341 if (mx != 2 || nel != oma_rz_count() - 2) fail("%s: expected exactly R3_sparse and R4_rns MAX_N%s", Q[i].id, "");
342 }
343 }
344 if (nrej == 0) fail("no named rejection%s%s", "", "");
345
346 /* separate-process reproduction files */
347 char repro_json[1024] = "";
348 size_t rl = 0;
349 int repro_all = 1;
350 {
351 char mine[NQ * 80] = "";
352 size_t ml = 0;
353 for (size_t i = 0; i < NQ; ++i) {
354 char h[65];
355 turing_hex(&ID[i], h);
356 ml += (size_t)snprintf(mine + ml, sizeof mine - ml, "%s %s\n", Q[i].id, h);
357 }
358 char list[512];
359 snprintf(list, sizeof list, "%s", repro ? repro : "");
360 size_t nfiles = 0;
361 for (char *tok = strtok(list, " "); tok; tok = strtok(NULL, " ")) {
362 char *eq = strchr(tok, '=');
363 if (!eq) return 2;
364 *eq = 0;
365 char *t = slurp(eq + 1);
366 int same = t && !strcmp(t, mine);
367 free(t);
368 if (!same) { repro_all = 0; fail("separate-process reproduction '%s' differs%s", tok, ""); }
369 rl += (size_t)snprintf(repro_json + rl, sizeof repro_json - rl, "%s\"%s\": \"%s\"", nfiles ? ", " : "", tok,
370 same ? "identical" : "DIFFERS");
371 nfiles++;
372 }
373 if (nfiles < 2) { repro_all = 0; fail("fewer than 2 separate-process reproductions%s%s", "", ""); }
374 }
375
376 if (check_expected("before wrapper write", f1, f2)) return 2;
377 int pass = g_nfail == 0;
378 FILE *o = fopen(out, "wx");
379 if (!o) { perror(out); return 2; }
380 char h[65];
381 turing_hex(&st->contract_digest, h);
382 fprintf(o, "{\n \"schema\": \"OMEGA_MIXED_ALGEBRA_DIGITAL_V1_GATE_V1\",\n \"gate\": \"MIXED_ALGEBRA_DIGITAL_V1\",\n");
383 fprintf(o, " \"spec\": \"spec/mixed-algebra-digital-v1.md\",\n \"run_commit\": ");
384 jstr(o, commit);
385 fprintf(o, ",\n \"tree_dirty\": %s,\n \"mode\": ", tree_dirty ? "true" : "false");
386 jstr(o, mode);
387 fprintf(o, ",\n \"bench_run_id\": ");
388 jstr(o, run_id);
389 fprintf(o, ",\n \"contract_digest\": \"%s\",\n \"selector\": \"%s\",\n", h, TURING_FIELD_SELECTOR);
390 fprintf(o, " \"energy\": \"not measured by this gate; bench energy rows are not ingested; pending omega#86\",\n");
391 fprintf(o, " \"receipts\": [\n");
392 for (size_t r = 0; r < 4; ++r) {
393 fprintf(o, " {\"role\": \"%s\", \"path\": ", ri[r].role);
394 jstr(o, ri[r].path);
395 fprintf(o, ", \"sha256\": \"%s\", \"schema\": ", ri[r].hex);
396 jstr(o, ri[r].schema);
397 fprintf(o, ", \"run_id\": ");
398 jstr(o, ri[r].run_id);
399 fprintf(o, ", \"run_commit\": ");
400 jstr(o, ri[r].run_commit);
401 fprintf(o, ", \"tree_dirty\": ");
402 jstr(o, ri[r].tree_dirty);
403 fprintf(o, ", \"mismatches\": ");
404 jstr(o, ri[r].mismatches);
405 fprintf(o, "}%s\n", r + 1 < 4 ? "," : "");
406 }
407 fprintf(o, " ],\n \"correctness\": [\n");
408 {
409 size_t first = 1;
410 for (char *l = ctext; *l;) {
411 char *nl = strchr(l, '\n');
412 if (nl) *nl = 0;
413 if (*l) {
414 fprintf(o, "%s ", first ? "" : ",\n");
415 jstr(o, l);
416 first = 0;
417 }
418 if (!nl) break;
419 l = nl + 1;
420 }
421 fprintf(o, "\n ],\n");
422 }
423 fprintf(o, " \"untracked_at_run\": [");
424 if (untracked) {
425 char *u = slurp(untracked);
426 size_t first = 1;
427 for (char *l = u; l && *l;) {
428 char *nl = strchr(l, '\n');
429 if (nl) *nl = 0;
430 if (*l) {
431 fprintf(o, "%s", first ? "" : ", ");
432 jstr(o, l);
433 first = 0;
434 }
435 if (!nl) break;
436 l = nl + 1;
437 }
438 free(u);
439 }
440 fprintf(o, "],\n \"queries\": [\n");
441 for (size_t i = 0; i < NQ; ++i) {
442 const turing_decision *d = &D[i];
443 char id[65], idc[65];
444 turing_hex(&ID[i], id);
445 turing_hex(&IDC[i], idc);
446 int ch = d->chosen, run = -1;
447 for (size_t k = 0; k < d->ncand; ++k)
448 if (is_eligible(d->reason[k]) && (int)k != ch && (run < 0 || d->cost_ps[k] < d->cost_ps[run])) run = (int)k;
449 double c1 = -1, c2 = -1;
450 if (ch >= 0) {
451 turing_digest rd1, rd2;
452 (void)turing_file_digest(f1, &rd1);
453 (void)turing_file_digest(f2, &rd2);
454 c1 = receipt_cost(st, &d->cand[ch], &rd1, d->cell_n, d->cell_m, d->pack);
455 c2 = receipt_cost(st, &d->cand[ch], &rd2, d->cell_n, d->cell_m, d->pack);
456 }
457 double rdiff = (c1 > 0 && c2 > 0) ? (c1 > c2 ? c1 / c2 - 1 : c2 / c1 - 1) : -1;
458 fprintf(o, " {\"id\": \"%s\", \"shape\": ", Q[i].id);
459 jstr(o, Q[i].what);
460 fprintf(o, ", \"n\": %" PRIu64 ", \"m\": %" PRIu64 ", \"sparsity\": 0.30, \"pack\": \"%s\",\n", Q[i].n, Q[i].m,
461 pack_name(Q[i].pack));
462 fprintf(o, " \"decision_digest\": \"%s\", \"verdict\": \"%s\", \"cell\": \"%s\", \"cell_n\": %" PRIu64
463 ", \"cell_m\": %" PRIu64 ",\n",
464 id, d->verdict, d->exact_cell ? "exact" : "nearest", d->cell_n, d->cell_m);
465 fprintf(o, " \"chosen\": \"%s\", \"chosen_family\": \"%s\", \"runner_up\": \"%s\", \"margin_pct\": %.2f, "
466 "\"noise_band_pct\": %.2f, \"margin_inside_band\": %s,\n",
467 ch >= 0 ? d->cand_rz[ch] : "none", ch >= 0 ? family_of(d->cand_rz[ch]) : "none",
468 run >= 0 ? d->cand_rz[run] : "none", d->margin_ppm / 1e4, d->band_ppm / 1e4,
469 d->margin_ppm <= d->band_ppm ? "true" : "false");
470 fprintf(o, " \"winner_cost_ns_run1\": %.3f, \"winner_cost_ns_run2\": %.3f, \"winner_run1_vs_run2_pct\": %.2f,\n",
471 c1 / 1e3, c2 / 1e3, rdiff * 100);
472 fprintf(o, " \"verify\": \"%s\", \"cites\": %zu, \"cites_fresh_only\": %s, \"reproduced_in_process\": %s,\n",
473 verify_ok[i] ? "all cited receipts verify" : why[i], d->ncite, cites_fresh[i] ? "true" : "false",
474 repro_in[i] ? "true" : "false");
475 fprintf(o, " \"continuity_ma3_decision_digest\": \"%s\", \"continuity_ma3_chosen\": \"%s\", "
476 "\"continuity_agrees\": %s,\n",
477 idc, DC[i].chosen >= 0 ? DC[i].cand_rz[DC[i].chosen] : "none",
478 (DC[i].chosen >= 0 && ch >= 0 && !strcmp(DC[i].cand_rz[DC[i].chosen], d->cand_rz[ch])) ? "true" : "false");
479 fprintf(o, " \"candidates\": [\n");
480 for (size_t k = 0; k < d->ncand; ++k) {
481 char sh[65];
482 turing_hex(&d->cand[k], sh);
483 fprintf(o, " {\"rz\": \"%s\", \"family\": \"%s\", \"spec\": \"%s\", \"reason\": \"%s\", "
484 "\"mean_cost_ns\": %.3f, \"weight_bytes\": %" PRIu64 "}%s\n",
485 d->cand_rz[k], family_of(d->cand_rz[k]), sh, turing_reason_name(d->reason[k]),
486 d->cost_ps[k] / 1e3, weight_bytes_of(st, &d->cand[k], d->cell_n, d->cell_m),
487 k + 1 < d->ncand ? "," : "");
488 }
489 fprintf(o, " ]}%s\n", i + 1 < NQ ? "," : "");
490 }
491 fprintf(o, " ],\n \"named_rejections\": %zu,\n \"reproduction_separate_process\": {%s},\n", nrej, repro_json);
492 fprintf(o, " \"reproduction_all_identical\": %s,\n \"failed_checks\": [", repro_all ? "true" : "false");
493 for (size_t f = 0; f < g_nfail && f < FAILS_MAX; ++f) {
494 fprintf(o, "%s", f ? ", " : "");
495 jstr(o, g_fails[f]);
496 }
497 fprintf(o, "],\n \"verdict\": \"MIXED_ALGEBRA_DIGITAL_V1 %s\"\n}\n", pass ? "PASS" : "FAIL");
498 fclose(o);
499 free(ctext);
500 for (size_t f = 0; f < g_nfail && f < FAILS_MAX; ++f) fprintf(stderr, "FAILED CHECK: %s\n", g_fails[f]);
501 printf("MIXED_ALGEBRA_DIGITAL_V1 %s (%zu failed checks)\n", pass ? "PASS" : "FAIL", g_nfail);
502 turing_store_free(st);
503 turing_store_free(st2);
504 turing_store_free(stc);
505 return pass ? 0 : 3;
506}
507
508int main(int argc, char **argv) {
509 if (argc == 4 && !strcmp(argv[1], "digests")) {
510 turing_store *st = turing_store_new();
511 if (!st || load(st, argv[2], argv[3]) || print_digests(stdout, st)) return 2;
512 turing_store_free(st);
513 return 0;
514 }
515 if (argc == 7 && !strcmp(argv[1], "receipt")) return do_receipt(argv[2], argv[3], argv[4], argv[5], argv[6]);
516 fprintf(stderr, "usage: %s digests FRESH1 FRESH2 | receipt OUT FRESH1 FRESH2 CONT1 CONT2\n", argv[0]);
517 return 2;
518}