Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/turing/replay.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / turing / replay.c` 203 lines ยท 8012 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 selector comparison on stored receipts (post hoc, no timed runs). 18 * Regret = cost(chosen) / cost(oracle) - 1, with costs = mean per-call cost 19 * over every receipt row of the exact queried cell. */ 20 #include "turing/select.h" 21 22 #include 23 #include 24 25 static int spec_index(const turing_store *st, const turing_digest *id) { 26 for (size_t k = 0; k < st->nspec; ++k) 27 if (turing_digest_eq(&st->spec_id[k], id)) return (int)k; 28 return -1; 29 } 30 31 double turing_cell_cost(const turing_store *st, size_t k, const turing_query *q) { 32 double sum = 0; 33 size_t rows = 0; 34 for (size_t i = 0; i < st->nev; ++i) { 35 const turing_evidence *e = &st->ev[i]; 36 if (e->n != q->n || e->m != q->m || e->sparsity_milli != q->sparsity_milli) continue; 37 if (!turing_digest_eq(&e->spec_id, &st->spec_id[k])) continue; 38 if (!e->verified) return -1; 39 sum += (double)turing_ev_cost(e, q->pack); 40 ++rows; 41 } 42 return rows ? sum / (double)rows : -1; 43 } 44 45 int turing_oracle(const turing_store *st, const turing_query *q, double *best_ps) { 46 int best = -1; 47 double bc = 0; 48 for (size_t k = 0; k < st->nspec; ++k) { 49 if (!st->spec[k].exact || q->n > st->spec[k].max_n) continue; 50 double c = turing_cell_cost(st, k, q); 51 if (c < 0) continue; 52 if (best < 0 || c < bc) best = (int)k, bc = c; 53 } 54 if (best >= 0 && best_ps) *best_ps = bc; 55 return best; 56 } 57 58 size_t turing_cells(const turing_store *st, turing_query *cells, size_t cap) { 59 size_t nc = 0; 60 for (size_t i = 0; i < st->nev; ++i) { 61 const turing_evidence *e = &st->ev[i]; 62 size_t j; 63 for (j = 0; j < nc; ++j) 64 if (cells[j].n == e->n && cells[j].m == e->m && cells[j].sparsity_milli == e->sparsity_milli) break; 65 if (j < nc) continue; 66 if (nc >= cap) return nc; 67 memset(&cells[nc], 0, sizeof cells[nc]); 68 cells[nc].n = e->n; 69 cells[nc].m = e->m; 70 cells[nc].sparsity_milli = e->sparsity_milli; 71 ++nc; 72 } 73 /* insertion sort by (m, n, s) for a stable, readable order */ 74 for (size_t i = 1; i < nc; ++i) { 75 turing_query t = cells[i]; 76 size_t j = i; 77 while (j > 0 && (cells[j - 1].m > t.m || (cells[j - 1].m == t.m && (cells[j - 1].n > t.n || 78 (cells[j - 1].n == t.n && cells[j - 1].sparsity_milli > t.sparsity_milli))))) { 79 cells[j] = cells[j - 1]; 80 --j; 81 } 82 cells[j] = t; 83 } 84 return nc; 85 } 86 87 static void acc(double r, double *mean, double *mx) { 88 *mean += r; 89 if (r > *mx) *mx = r; 90 } 91 92 static double regret_of(const turing_store *st, const turing_query *q, int k, double oracle) { 93 if (k < 0) return 1e9; 94 double c = turing_cell_cost(st, (size_t)k, q); 95 return c < 0 ? 1e9 : c / oracle - 1.0; 96 } 97 98 static int field_pick(const turing_store *st, turing_field_rule rule, const turing_query *q, turing_decision *d) { 99 return rule == TURING_FIELD_V0_RETIRED ? turing_field_select_v0_retired(st, q, NULL, NULL, d) 100 : turing_field_select(st, q, NULL, d); 101 } 102 103 int turing_compare(const turing_store *st, turing_field_rule rule, const turing_query *qs, size_t nq, int loo, 104 uint64_t seed, turing_regret *out) { 105 memset(out, 0, sizeof *out); 106 turing_history *h = malloc(sizeof *h); 107 turing_decision *fd = malloc(sizeof *fd), *hd = malloc(sizeof *hd); 108 int rc = -1; 109 if (!h || !fd || !hd) goto done; 110 if (!loo) { 111 turing_history_init(h, seed); 112 if (turing_history_load(h, st, NULL) != 0) goto done; 113 } 114 for (size_t i = 0; i < nq; ++i) { 115 turing_query q = qs[i]; 116 q.exclude_exact_cell = loo; 117 double oracle; 118 if (turing_oracle(st, &q, &oracle) < 0) goto done; 119 if (loo) { 120 turing_history_init(h, seed); 121 if (turing_history_load(h, st, &q) != 0) goto done; 122 } 123 if (field_pick(st, rule, &q, fd) != 0) goto done; 124 int hk = turing_history_select(h, st, &q, hd); 125 int fk = spec_index(st, &fd->cand[fd->chosen]); 126 acc(regret_of(st, &q, fk, oracle), &out->field_mean, &out->field_max); 127 acc(regret_of(st, &q, hk, oracle), &out->hist_mean, &out->hist_max); 128 char why[160]; 129 out->field_cites_ok += turing_decision_verify(st, fd, why, sizeof why) == 0; 130 out->hist_cites_ok += turing_decision_verify(st, hd, why, sizeof why) == 0; 131 out->agree += fk == hk; 132 ++out->decisions; 133 } 134 if (out->decisions) { 135 out->field_mean /= (double)out->decisions; 136 out->hist_mean /= (double)out->decisions; 137 } 138 rc = 0; 139 done: 140 free(h); 141 free(fd); 142 free(hd); 143 return rc; 144 } 145 146 int turing_compare_online(const turing_store *st, turing_field_rule rule, const turing_query *qs, size_t nq, 147 size_t rounds, uint64_t seed, turing_regret *out) { 148 memset(out, 0, sizeof *out); 149 if (nq == 0 || nq > 256 || st->nreceipt == 0) return -1; 150 turing_history *h = malloc(sizeof *h); 151 turing_decision *fd = malloc(sizeof *fd), *hd = malloc(sizeof *hd); 152 size_t *order = malloc(nq * sizeof *order); 153 int rc = -1; 154 if (!h || !fd || !hd || !order) goto done; 155 turing_history_init(h, seed); 156 uint64_t s = seed; 157 /* Observation counter per (spec, query) picks which receipt row to reveal. */ 158 static unsigned seen[TURING_MAX_SPECS][256]; 159 memset(seen, 0, sizeof seen); 160 for (size_t r = 0; r < rounds; ++r) { 161 for (size_t i = 0; i < nq; ++i) order[i] = i; 162 for (size_t i = nq; i > 1; --i) { 163 size_t j = (size_t)(turing_splitmix64(&s) % i); 164 size_t t = order[i - 1]; 165 order[i - 1] = order[j]; 166 order[j] = t; 167 } 168 for (size_t t = 0; t < nq; ++t) { 169 const turing_query *q = &qs[order[t]]; 170 double oracle; 171 if (turing_oracle(st, q, &oracle) < 0) goto done; 172 if (field_pick(st, rule, q, fd) != 0) goto done; 173 int hk = turing_history_select(h, st, q, hd); 174 if (hk < 0) goto done; 175 /* Reveal one stored row for the control arm's pick, rotating receipts. */ 176 uint64_t rows[TURING_MAX_RECEIPTS]; 177 size_t nrows = 0; 178 for (size_t i = 0; i < st->nev && nrows < TURING_MAX_RECEIPTS; ++i) { 179 const turing_evidence *e = &st->ev[i]; 180 if (e->n == q->n && e->m == q->m && e->sparsity_milli == q->sparsity_milli && 181 turing_digest_eq(&e->spec_id, &st->spec_id[hk])) 182 rows[nrows++] = turing_ev_cost(e, q->pack); 183 } 184 if (nrows == 0) goto done; 185 uint64_t cost = rows[seen[hk][order[t]]++ % nrows]; 186 if (turing_history_observe(h, st, (size_t)hk, q->n, q->m, q->pack, cost) != 0) goto done; 187 int fk = spec_index(st, &fd->cand[fd->chosen]); 188 acc(regret_of(st, q, fk, oracle), &out->field_mean, &out->field_max); 189 acc(regret_of(st, q, hk, oracle), &out->hist_mean, &out->hist_max); 190 out->agree += fk == hk; 191 ++out->decisions; 192 } 193 } 194 out->field_mean /= (double)out->decisions; 195 out->hist_mean /= (double)out->decisions; 196 rc = 0; 197 done: 198 free(h); 199 free(fd); 200 free(hd); 201 free(order); 202 return rc; 203 }