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
omega-mv / src / turing / field_select.c
| 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 Field selector v1: the record-keeping layer |
| 18 | * (docs/turing/TURING_W0_PROPOSAL.md K.2, K.7). |
| 19 | * |
| 20 | * Selection is NOT novel here: the choice is made by turing_rank_min_cost, the |
| 21 | * control arm's rule (history_selector.c). What the Field adds is the record. |
| 22 | * |
| 23 | * Rule, in order: |
| 24 | * 1. Candidates = every spec in the store. Recorded filters with reason |
| 25 | * codes: CONTRACT_MISMATCH (spec names another contract digest), |
| 26 | * NOT_EXACT, MAX_N (query n above the spec's max_n). |
| 27 | * 2. Usable evidence = rows whose receipt file still hashes to the digest the |
| 28 | * row records (re-hashed at decision time), minus the exact query cell |
| 29 | * under leave-one-cell-out. |
| 30 | * 3. Footprint = (n, m) as in the control arm (sparsity changes values, not |
| 31 | * sizes). The query's own footprint when it has usable evidence for an |
| 32 | * eligible candidate, else the nearest measured one by |log2 n ratio| + |
| 33 | * |log2 m ratio|; ties -> smallest (n, m). |
| 34 | * 4. Per candidate over its rows in that footprint: NO_EVIDENCE, |
| 35 | * UNVERIFIED_RUN (a row not oracle-verified), CONTENTION_FORCED (a forced |
| 36 | * block), TIER (tier text not E2/E3/E4). Filters exclude; they never |
| 37 | * reorder. |
| 38 | * 5. Ranking: the surviving rows, in store order, feed one ranking-core |
| 39 | * bucket; turing_rank_min_cost picks. On the stored grid, with every |
| 40 | * filter passing, this is bit-for-bit the control arm's warm choice. |
| 41 | * 6. Recorded, not used to choose: margin to the runner-up and the noise band |
| 42 | * (largest within-run spread of the chosen and runner-up rows). |
| 43 | * 7. The decision cites every evidence row the ranking used. |
| 44 | */ |
| 45 | #include "turing/select.h" |
| 46 | |
| 47 | #include <math.h> |
| 48 | #include <stdio.h> |
| 49 | #include <string.h> |
| 50 | |
| 51 | static double foot_dist(const turing_query *q, uint64_t n, uint64_t m) { |
| 52 | return fabs(log2((double)n / (double)q->n)) + fabs(log2((double)m / (double)q->m)); |
| 53 | } |
| 54 | |
| 55 | static int is_excluded(const turing_query *q, const turing_evidence *e) { |
| 56 | return q->exclude_exact_cell && e->n == q->n && e->m == q->m && e->sparsity_milli == q->sparsity_milli; |
| 57 | } |
| 58 | |
| 59 | static int tier_ok(const char *t) { return !strcmp(t, "E2") || !strcmp(t, "E3") || !strcmp(t, "E4"); } |
| 60 | |
| 61 | /* Within-run relative spread of one row for a pack mode. */ |
| 62 | static double row_noise(const turing_evidence *e, int pack) { |
| 63 | double c = (double)turing_ev_cost(e, pack); |
| 64 | if (pack == TURING_PACK_PER_CALL && c > 0) |
| 65 | return (e->noise_ppm / 1e6 * (double)e->median_ps + e->pack_noise_ppm / 1e6 * (double)e->pack_ps) / c; |
| 66 | return e->noise_ppm / 1e6; |
| 67 | } |
| 68 | |
| 69 | int turing_field_select(const turing_store *st, const turing_query *q, const turing_digest *supersedes, |
| 70 | 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); |
| 75 | snprintf(d->constraints, sizeof d->constraints, |
| 76 | "exact contract; tier in {E2,E3,E4}; receipts verify; rank = min mean cost per (n,m,pack)"); |
| 77 | d->n = q->n; |
| 78 | d->m = q->m; |
| 79 | d->sparsity_milli = q->sparsity_milli; |
| 80 | d->pack = q->pack; |
| 81 | d->chosen = -1; |
| 82 | if (supersedes) { |
| 83 | d->has_supersedes = 1; |
| 84 | d->supersedes = *supersedes; |
| 85 | } |
| 86 | snprintf(d->tie_resolution, sizeof d->tie_resolution, "none"); |
| 87 | |
| 88 | /* Receipts re-hashed now. */ |
| 89 | int receipt_ok[TURING_MAX_RECEIPTS] = {0}; |
| 90 | for (size_t r = 0; r < st->nreceipt; ++r) { |
| 91 | turing_digest fd; |
| 92 | receipt_ok[r] = turing_file_digest(st->receipt[r].path, &fd) == 0 && turing_digest_eq(&fd, &st->receipt[r].digest); |
| 93 | } |
| 94 | |
| 95 | /* 1. Recorded contract / exactness / domain filters. */ |
| 96 | int eligible[TURING_MAX_CAND] = {0}; |
| 97 | d->ncand = st->nspec; |
| 98 | for (size_t k = 0; k < st->nspec; ++k) { |
| 99 | const turing_rz_spec *s = &st->spec[k]; |
| 100 | d->cand[k] = st->spec_id[k]; |
| 101 | snprintf(d->cand_rz[k], sizeof d->cand_rz[k], "%s", s->rz_id); |
| 102 | if (!turing_digest_eq(&s->contract_digest, &st->contract_digest)) |
| 103 | d->reason[k] = TURING_R_CONTRACT_MISMATCH; |
| 104 | else if (s->exact != 1) |
| 105 | d->reason[k] = TURING_R_NOT_EXACT; |
| 106 | else if (q->n > s->max_n) |
| 107 | d->reason[k] = TURING_R_MAX_N; |
| 108 | else { |
| 109 | d->reason[k] = TURING_R_NO_EVIDENCE; |
| 110 | eligible[k] = 1; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* 2. Usable evidence rows; spec index per row. */ |
| 115 | unsigned char usable[TURING_MAX_EVIDENCE]; |
| 116 | int row_spec[TURING_MAX_EVIDENCE]; |
| 117 | int any_unusable_receipt[TURING_MAX_CAND] = {0}; |
| 118 | for (size_t i = 0; i < st->nev; ++i) { |
| 119 | const turing_evidence *e = &st->ev[i]; |
| 120 | usable[i] = 0; |
| 121 | row_spec[i] = -1; |
| 122 | for (size_t k = 0; k < st->nspec; ++k) |
| 123 | if (turing_digest_eq(&st->spec_id[k], &e->spec_id)) row_spec[i] = (int)k; |
| 124 | if (row_spec[i] < 0 || is_excluded(q, e)) continue; |
| 125 | int ok = 0; |
| 126 | for (size_t r = 0; r < st->nreceipt; ++r) |
| 127 | if (turing_digest_eq(&st->receipt[r].digest, &e->receipt_digest)) ok = receipt_ok[r]; |
| 128 | if (!ok) any_unusable_receipt[row_spec[i]] = 1; |
| 129 | else if (eligible[row_spec[i]]) usable[i] = 1; |
| 130 | } |
| 131 | |
| 132 | /* 3. Footprint. */ |
| 133 | int have = 0; |
| 134 | double best = 0; |
| 135 | for (size_t i = 0; i < st->nev; ++i) { |
| 136 | if (!usable[i]) continue; |
| 137 | const turing_evidence *e = &st->ev[i]; |
| 138 | double dd = foot_dist(q, e->n, e->m); |
| 139 | if (!have || dd < best - 1e-12 || |
| 140 | (fabs(dd - best) <= 1e-12 && (e->n < d->cell_n || (e->n == d->cell_n && e->m < d->cell_m)))) { |
| 141 | have = 1; |
| 142 | best = dd; |
| 143 | d->cell_n = e->n; |
| 144 | d->cell_m = e->m; |
| 145 | } |
| 146 | } |
| 147 | d->cell_sparsity_milli = TURING_SPARSITY_ANY; |
| 148 | if (!have) { |
| 149 | for (size_t k = 0; k < st->nspec; ++k) |
| 150 | if (eligible[k] && any_unusable_receipt[k]) d->reason[k] = TURING_R_RECEIPT_UNVERIFIED; |
| 151 | snprintf(d->verdict, sizeof d->verdict, "NONE"); |
| 152 | return 1; |
| 153 | } |
| 154 | d->exact_cell = d->cell_n == q->n && d->cell_m == q->m; |
| 155 | |
| 156 | /* 4. Per-candidate run / contention / tier filters over the footprint. */ |
| 157 | int rows[TURING_MAX_CAND] = {0}, bad[TURING_MAX_CAND]; |
| 158 | for (size_t k = 0; k < st->nspec; ++k) bad[k] = -1; |
| 159 | for (size_t i = 0; i < st->nev; ++i) { |
| 160 | const turing_evidence *e = &st->ev[i]; |
| 161 | if (!usable[i] || e->n != d->cell_n || e->m != d->cell_m) continue; |
| 162 | int k = row_spec[i]; |
| 163 | rows[k]++; |
| 164 | if (!e->verified) bad[k] = TURING_R_UNVERIFIED_RUN; |
| 165 | else if (e->forced) bad[k] = TURING_R_CONTENTION_FORCED; |
| 166 | else if (!tier_ok(e->tier)) bad[k] = TURING_R_TIER; |
| 167 | } |
| 168 | int ranked[TURING_MAX_CAND] = {0}; |
| 169 | for (size_t k = 0; k < st->nspec; ++k) { |
| 170 | if (!eligible[k]) continue; |
| 171 | if (rows[k] == 0) |
| 172 | d->reason[k] = any_unusable_receipt[k] ? TURING_R_RECEIPT_UNVERIFIED : TURING_R_NO_EVIDENCE; |
| 173 | else if (bad[k] >= 0) |
| 174 | d->reason[k] = (turing_reason)bad[k]; |
| 175 | else |
| 176 | ranked[k] = 1, d->reason[k] = TURING_R_RANKED; |
| 177 | } |
| 178 | |
| 179 | /* 5. Ranking core over the surviving rows, in store order; cite each. */ |
| 180 | turing_hist_bucket b; |
| 181 | turing_bucket_reset(&b, d->cell_n, d->cell_m, q->pack); |
| 182 | for (size_t i = 0; i < st->nev; ++i) { |
| 183 | const turing_evidence *e = &st->ev[i]; |
| 184 | if (!usable[i] || e->n != d->cell_n || e->m != d->cell_m || !ranked[row_spec[i]]) continue; |
| 185 | if (d->ncite >= TURING_MAX_CITE) return -1; |
| 186 | turing_bucket_observe(&b, (size_t)row_spec[i], turing_ev_cost(e, q->pack)); |
| 187 | d->cite[d->ncite++] = st->ev_id[i]; |
| 188 | } |
| 189 | int chosen = turing_rank_min_cost(&b, ranked, st->nspec); |
| 190 | if (chosen < 0) { |
| 191 | snprintf(d->verdict, sizeof d->verdict, "NONE"); |
| 192 | d->ncite = 0; |
| 193 | return 1; |
| 194 | } |
| 195 | for (size_t k = 0; k < st->nspec; ++k) |
| 196 | if (ranked[k]) d->cost_ps[k] = (uint64_t)llround(b.mean_ps[k]); |
| 197 | |
| 198 | /* 6. Recorded margin and noise band (not used to choose). */ |
| 199 | int runner = -1; |
| 200 | for (size_t k = 0; k < st->nspec; ++k) |
| 201 | if (ranked[k] && (int)k != chosen && (runner < 0 || b.mean_ps[k] < b.mean_ps[runner])) runner = (int)k; |
| 202 | double band = 0; |
| 203 | for (size_t i = 0; i < st->nev; ++i) { |
| 204 | const turing_evidence *e = &st->ev[i]; |
| 205 | if (!usable[i] || e->n != d->cell_n || e->m != d->cell_m) continue; |
| 206 | if (row_spec[i] != chosen && row_spec[i] != runner) continue; |
| 207 | double w = row_noise(e, q->pack); |
| 208 | if (w > band) band = w; |
| 209 | } |
| 210 | d->band_ppm = (uint32_t)llround(band * 1e6); |
| 211 | d->margin_ppm = runner >= 0 ? (uint32_t)llround((b.mean_ps[runner] / b.mean_ps[chosen] - 1.0) * 1e6) : 0; |
| 212 | |
| 213 | d->chosen = chosen; |
| 214 | d->reason[chosen] = TURING_R_CHOSEN; |
| 215 | snprintf(d->verdict, sizeof d->verdict, "CHOSEN"); |
| 216 | return 0; |
| 217 | } |