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 / field_select_v0_retired.c

246 lines · 10381 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/* RETIRED: TURING Field selector V0 with the ADR 0019 9.1 tie rule
18 * (docs/turing/TURING_W0_PROPOSAL.md K.2, K.6, K.7).
19 *
20 * Kept ONLY to reproduce the recorded kill-test FAIL of 2026-09-29 (K.6) in
21 * test-turing and in the turing-field dry run. Do not use it for selection:
22 * the live Field selector is field_select.c (turing.field.v1), which ranks
23 * with the control arm's rule. Frozen: any change here changes the record.
24 *
25 * Rule, in order:
26 * 1. Candidates = every spec in the store. Filters with reason codes:
27 * CONTRACT_MISMATCH (spec names another contract digest), NOT_EXACT,
28 * MAX_N (query n above the spec's max_n).
29 * 2. Usable evidence = rows whose receipt file still hashes to the digest the
30 * row records (re-hashed at decision time).
31 * 3. Cell = the measured (n, m, sparsity) nearest the query among cells with
32 * usable evidence for an eligible candidate: distance |log2 n ratio| +
33 * |log2 m ratio| + 4 |sparsity difference|; ties -> smallest (n, m, s).
34 * 4. Per candidate at that cell: NO_EVIDENCE, UNVERIFIED_RUN (a row not
35 * oracle-verified), CONTENTION_FORCED (a forced block), TIER (tier text not
36 * E2/E3/E4), else cost = mean over rows of the per-call cost.
37 * 5. Noise band = max over cheapest and runner-up of within-run spread and
38 * across-run spread; tied set = costs within the band of the cheapest;
39 * TIE when it has more than one member, resolved only inside it:
40 * incumbent, else the reference R1_plain, else the cheapest.
41 * 6. The decision cites every evidence digest used in step 4.
42 */
43#include "turing/select.h"
44
45#include <inttypes.h>
46#include <math.h>
47#include <stdio.h>
48#include <string.h>
49
50static double cell_dist(const turing_query *q, uint64_t n, uint64_t m, uint32_t s) {
51 double dn = fabs(log2((double)n / (double)q->n));
52 double dm = fabs(log2((double)m / (double)q->m));
53 double ds = fabs((double)s - (double)q->sparsity_milli) / 1000.0;
54 return dn + dm + 4.0 * ds;
55}
56
57static int cell_less(uint64_t n1, uint64_t m1, uint32_t s1, uint64_t n2, uint64_t m2, uint32_t s2) {
58 if (n1 != n2) return n1 < n2;
59 if (m1 != m2) return m1 < m2;
60 return s1 < s2;
61}
62
63static int is_excluded(const turing_query *q, const turing_evidence *e) {
64 return q->exclude_exact_cell && e->n == q->n && e->m == q->m && e->sparsity_milli == q->sparsity_milli;
65}
66
67static int tier_ok(const char *t) { return !strcmp(t, "E2") || !strcmp(t, "E3") || !strcmp(t, "E4"); }
68
69int turing_field_select_v0_retired(const turing_store *st, const turing_query *q, const char *incumbent_rz,
70 const turing_digest *supersedes, turing_decision *d) {
71 if (!st || !q || !d || q->n == 0 || q->m == 0 || st->nspec > TURING_MAX_CAND) return -1;
72 memset(d, 0, sizeof *d);
73 d->contract_digest = st->contract_digest;
74 snprintf(d->selector, sizeof d->selector, "%s", TURING_FIELD_SELECTOR_V0);
75 snprintf(d->constraints, sizeof d->constraints, "exact contract; tier in {E2,E3,E4}; receipts verify");
76 d->n = q->n;
77 d->m = q->m;
78 d->sparsity_milli = q->sparsity_milli;
79 d->pack = q->pack;
80 d->chosen = -1;
81 if (supersedes) {
82 d->has_supersedes = 1;
83 d->supersedes = *supersedes;
84 }
85 snprintf(d->tie_resolution, sizeof d->tie_resolution, "none");
86
87 /* Receipts re-hashed now. */
88 int receipt_ok[TURING_MAX_RECEIPTS] = {0};
89 for (size_t r = 0; r < st->nreceipt; ++r) {
90 turing_digest fd;
91 receipt_ok[r] = turing_file_digest(st->receipt[r].path, &fd) == 0 && turing_digest_eq(&fd, &st->receipt[r].digest);
92 }
93
94 int eligible[TURING_MAX_CAND] = {0};
95 d->ncand = st->nspec;
96 for (size_t k = 0; k < st->nspec; ++k) {
97 const turing_rz_spec *s = &st->spec[k];
98 d->cand[k] = st->spec_id[k];
99 snprintf(d->cand_rz[k], sizeof d->cand_rz[k], "%s", s->rz_id);
100 if (!turing_digest_eq(&s->contract_digest, &st->contract_digest))
101 d->reason[k] = TURING_R_CONTRACT_MISMATCH;
102 else if (s->exact != 1)
103 d->reason[k] = TURING_R_NOT_EXACT;
104 else if (q->n > s->max_n)
105 d->reason[k] = TURING_R_MAX_N;
106 else {
107 d->reason[k] = TURING_R_NO_EVIDENCE;
108 eligible[k] = 1;
109 }
110 }
111
112 /* Usable evidence rows. */
113 unsigned char usable[TURING_MAX_EVIDENCE];
114 int any_unusable_receipt[TURING_MAX_CAND] = {0};
115 for (size_t i = 0; i < st->nev; ++i) {
116 const turing_evidence *e = &st->ev[i];
117 usable[i] = 0;
118 if (is_excluded(q, e)) continue;
119 int ok = 0;
120 for (size_t r = 0; r < st->nreceipt; ++r)
121 if (turing_digest_eq(&st->receipt[r].digest, &e->receipt_digest)) ok = receipt_ok[r];
122 for (size_t k = 0; k < st->nspec; ++k)
123 if (turing_digest_eq(&st->spec_id[k], &e->spec_id)) {
124 if (!ok) any_unusable_receipt[k] = 1;
125 else if (eligible[k]) usable[i] = 1;
126 }
127 }
128
129 /* Nearest cell. */
130 int have = 0;
131 double best = 0;
132 for (size_t i = 0; i < st->nev; ++i) {
133 if (!usable[i]) continue;
134 const turing_evidence *e = &st->ev[i];
135 double dd = cell_dist(q, e->n, e->m, e->sparsity_milli);
136 if (!have || dd < best - 1e-12 ||
137 (fabs(dd - best) <= 1e-12 &&
138 cell_less(e->n, e->m, e->sparsity_milli, d->cell_n, d->cell_m, d->cell_sparsity_milli))) {
139 have = 1;
140 best = dd;
141 d->cell_n = e->n;
142 d->cell_m = e->m;
143 d->cell_sparsity_milli = e->sparsity_milli;
144 }
145 }
146 if (!have) {
147 for (size_t k = 0; k < st->nspec; ++k)
148 if (eligible[k] && any_unusable_receipt[k]) d->reason[k] = TURING_R_RECEIPT_UNVERIFIED;
149 snprintf(d->verdict, sizeof d->verdict, "NONE");
150 return 1;
151 }
152 d->exact_cell = d->cell_n == q->n && d->cell_m == q->m && d->cell_sparsity_milli == q->sparsity_milli;
153
154 /* Per-candidate cost at the cell. */
155 double cost[TURING_MAX_CAND] = {0}, spread[TURING_MAX_CAND] = {0};
156 int measured[TURING_MAX_CAND] = {0};
157 for (size_t k = 0; k < st->nspec; ++k) {
158 if (!eligible[k]) continue;
159 double sum = 0, lo = 0, hi = 0, within = 0;
160 size_t rows = 0;
161 int bad = -1;
162 size_t cite0 = d->ncite;
163 for (size_t i = 0; i < st->nev; ++i) {
164 const turing_evidence *e = &st->ev[i];
165 if (!usable[i] || !turing_digest_eq(&e->spec_id, &st->spec_id[k]) || e->n != d->cell_n ||
166 e->m != d->cell_m || e->sparsity_milli != d->cell_sparsity_milli)
167 continue;
168 if (!e->verified) bad = TURING_R_UNVERIFIED_RUN;
169 else if (e->forced) bad = TURING_R_CONTENTION_FORCED;
170 else if (!tier_ok(e->tier)) bad = TURING_R_TIER;
171 double c = (double)turing_ev_cost(e, q->pack);
172 double w = e->noise_ppm / 1e6;
173 if (q->pack == TURING_PACK_PER_CALL && c > 0)
174 w = (e->noise_ppm / 1e6 * (double)e->median_ps + e->pack_noise_ppm / 1e6 * (double)e->pack_ps) / c;
175 if (rows == 0 || c < lo) lo = c;
176 if (rows == 0 || c > hi) hi = c;
177 if (w > within) within = w;
178 sum += c;
179 ++rows;
180 if (d->ncite >= TURING_MAX_CITE) return -1;
181 d->cite[d->ncite++] = st->ev_id[i];
182 }
183 if (rows == 0) {
184 d->reason[k] = any_unusable_receipt[k] ? TURING_R_RECEIPT_UNVERIFIED : TURING_R_NO_EVIDENCE;
185 continue;
186 }
187 if (bad >= 0) {
188 d->reason[k] = (turing_reason)bad;
189 d->ncite = cite0; /* only cite what the choice rests on */
190 continue;
191 }
192 cost[k] = sum / (double)rows;
193 double across = cost[k] > 0 ? (hi - lo) / cost[k] : 0;
194 spread[k] = within > across ? within : across;
195 measured[k] = 1;
196 d->reason[k] = TURING_R_RANKED;
197 d->cost_ps[k] = (uint64_t)llround(cost[k]);
198 }
199
200 int cheap = -1, runner = -1;
201 for (size_t k = 0; k < st->nspec; ++k) {
202 if (!measured[k]) continue;
203 if (cheap < 0 || cost[k] < cost[cheap]) {
204 runner = cheap;
205 cheap = (int)k;
206 } else if (runner < 0 || cost[k] < cost[runner]) {
207 runner = (int)k;
208 }
209 }
210 if (cheap < 0) {
211 snprintf(d->verdict, sizeof d->verdict, "NONE");
212 d->ncite = 0;
213 return 1;
214 }
215 double band = spread[cheap];
216 if (runner >= 0 && spread[runner] > band) band = spread[runner];
217 d->band_ppm = (uint32_t)llround(band * 1e6);
218 d->margin_ppm = runner >= 0 ? (uint32_t)llround((cost[runner] / cost[cheap] - 1.0) * 1e6) : 0;
219
220 int tied[TURING_MAX_CAND] = {0}, ntied = 0;
221 for (size_t k = 0; k < st->nspec; ++k)
222 if (measured[k] && cost[k] <= cost[cheap] * (1.0 + band)) tied[k] = 1, ++ntied;
223
224 int chosen = cheap;
225 if (ntied > 1) {
226 snprintf(d->verdict, sizeof d->verdict, "TIE");
227 int inc = incumbent_rz ? turing_find_spec(st, incumbent_rz) : -1;
228 int ref = turing_find_spec(st, TURING_REFERENCE_RZ);
229 if (inc >= 0 && tied[inc]) {
230 chosen = inc;
231 snprintf(d->tie_resolution, sizeof d->tie_resolution, "incumbent");
232 } else if (ref >= 0 && tied[ref]) {
233 chosen = ref;
234 snprintf(d->tie_resolution, sizeof d->tie_resolution, "reference");
235 } else {
236 snprintf(d->tie_resolution, sizeof d->tie_resolution, "cheapest");
237 }
238 for (size_t k = 0; k < st->nspec; ++k)
239 if (tied[k]) d->reason[k] = TURING_R_TIED;
240 } else {
241 snprintf(d->verdict, sizeof d->verdict, "CHOSEN");
242 }
243 d->chosen = chosen;
244 d->reason[chosen] = TURING_R_CHOSEN;
245 return 0;
246}