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 / select.h

150 lines · 7126 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 selectors over Field records (docs/turing/TURING_W0_PROPOSAL.md
18 * sections K.2, K.3, K.4, K.7).
19 *
20 * - Ranking core (history_selector.c): minimum expected cost = minimum
21 * running mean of observed per-call cost per (spec, footprint n x m x pack),
22 * lowest spec index on an exact tie. Shared by both selectors below.
23 * - Control arm (history_selector.c): StarPU-style history model around the
24 * ranking core, with a seeded round-robin cold start.
25 * - Field selector v1 (field_select.c): the record-keeping layer. It ranks
26 * with the ranking core (the control arm's rule, not a different rule),
27 * keeps contract / exactness / domain / receipt / run-verification /
28 * contention / tier filters as recorded reason codes, and emits an
29 * immutable decision record citing every evidence row the ranking used.
30 * - RETIRED Field selector v0 (field_select_v0_retired.c): the ADR 0019 9.1
31 * tie rule inside the noise band. It FAILED the pre-registered kill test
32 * (K.6) and is kept only to reproduce that recorded FAIL.
33 *
34 * All run post hoc on stored receipts. No timed runs.
35 */
36#ifndef TURING_SELECT_H
37#define TURING_SELECT_H
38
39#include "turing/field.h"
40
41typedef struct {
42 uint64_t n, m;
43 uint32_t sparsity_milli;
44 int pack; /* TURING_PACK_* */
45 int exclude_exact_cell; /* leave-one-cell-out: ignore evidence for (n, m, sparsity) */
46} turing_query;
47
48/* ----------------------------------------------------------- ranking core */
49
50#define TURING_HIST_MAX_BUCKETS 64
51
52typedef struct {
53 uint64_t n, m;
54 int pack;
55 uint32_t count[TURING_MAX_SPECS];
56 double mean_ps[TURING_MAX_SPECS];
57 uint32_t order[TURING_MAX_SPECS]; /* control arm only: seeded shuffle of spec indices */
58 uint32_t cursor;
59} turing_hist_bucket;
60
61/* Empty bucket for footprint (n, m, pack); order[] left as identity. */
62void turing_bucket_reset(turing_hist_bucket *b, uint64_t n, uint64_t m, int pack);
63/* Fold one observed per-call cost of spec index k into the running mean. */
64void turing_bucket_observe(turing_hist_bucket *b, size_t k, uint64_t cost_ps);
65/* THE ranking rule: among k < nspec with allowed[k] and count[k] > 0, the
66 * minimum running mean; lowest index on an exact tie. Returns k or -1. */
67int turing_rank_min_cost(const turing_hist_bucket *b, const int *allowed, size_t nspec);
68
69/* ------------------------------------------------------ Field selector v1 */
70
71#define TURING_FIELD_SELECTOR "turing.field.v1"
72#define TURING_SPARSITY_ANY 0xFFFFFFFFu /* decision cell covers every sparsity of the footprint */
73
74/* Filters with reason codes, footprint = query (n, m) when it has usable
75 * evidence, else the nearest measured footprint; ranking by
76 * turing_rank_min_cost over the filtered rows in store order; cites every
77 * row the ranking used. supersedes may be NULL. Returns 0 when a realization
78 * was chosen, -1 on error, 1 when no candidate survived. */
79int turing_field_select(const turing_store *st, const turing_query *q, const turing_digest *supersedes,
80 turing_decision *out);
81
82/* ------------------------------------------ RETIRED Field selector v0 */
83
84#define TURING_FIELD_SELECTOR_V0 "turing.field.v0"
85#define TURING_REFERENCE_RZ "R1_plain" /* ADR 0019 9.1 digital reference */
86
87/* RETIRED (K.6 FAIL). Reproduces the recorded dry run only. */
88int turing_field_select_v0_retired(const turing_store *st, const turing_query *q, const char *incumbent_rz,
89 const turing_digest *supersedes, turing_decision *out);
90
91/* ----------------------------------------------------------- control arm */
92
93#define TURING_HISTORY_SELECTOR "turing.history.v0"
94
95typedef struct {
96 uint64_t seed;
97 size_t nbucket;
98 turing_hist_bucket bucket[TURING_HIST_MAX_BUCKETS];
99} turing_history;
100
101void turing_history_init(turing_history *h, uint64_t seed);
102/* Feed one observed per-call cost for spec index k at footprint (n, m, pack). */
103int turing_history_observe(turing_history *h, const turing_store *st, size_t k, uint64_t n, uint64_t m, int pack,
104 uint64_t cost_ps);
105/* Warm start: observe every evidence row (both pack modes), optionally
106 * skipping one exact cell. */
107int turing_history_load(turing_history *h, const turing_store *st, const turing_query *skip);
108/* Choose. Fills a decision record (it cites no evidence: the control arm
109 * keeps only running means). Returns the chosen spec index or -1. */
110int turing_history_select(turing_history *h, const turing_store *st, const turing_query *q, turing_decision *out);
111
112/* --------------------------------------------------------------- replay */
113
114uint64_t turing_splitmix64(uint64_t *state);
115
116/* Oracle: mean per-call cost over every verified receipt row of the exact
117 * cell; cheapest spec. Returns spec index or -1. */
118int turing_oracle(const turing_store *st, const turing_query *q, double *best_ps);
119/* Mean per-call cost of spec k at the exact cell, or <0 when unmeasured. */
120double turing_cell_cost(const turing_store *st, size_t k, const turing_query *q);
121
122typedef struct {
123 size_t decisions;
124 double field_mean, field_max;
125 double hist_mean, hist_max;
126 size_t field_cites_ok; /* decisions whose cited receipts verify */
127 size_t hist_cites_ok;
128 size_t agree; /* same realization chosen */
129} turing_regret;
130
131/* Distinct measured cells (n, m, sparsity) in the store, sorted. */
132size_t turing_cells(const turing_store *st, turing_query *cells, size_t cap);
133
134/* Which Field selector a comparison scores. */
135typedef enum {
136 TURING_FIELD_V1 = 0, /* live: control arm's ranking rule + records */
137 TURING_FIELD_V0_RETIRED = 1, /* retired tie rule: reproduces the K.6 FAIL */
138} turing_field_rule;
139
140/* In-sample (control warm on the same receipts) and leave-one-cell-out over
141 * the given queries (pack mode taken from each query). */
142int turing_compare(const turing_store *st, turing_field_rule rule, const turing_query *qs, size_t nq,
143 int leave_one_out, uint64_t seed, turing_regret *out);
144/* Cold online replay: the control arm starts empty and learns from the costs
145 * it observes; T queries drawn from qs in seeded shuffled rounds. Field
146 * decisions are static (stored evidence). */
147int turing_compare_online(const turing_store *st, turing_field_rule rule, const turing_query *qs, size_t nq,
148 size_t rounds, uint64_t seed, turing_regret *out);
149
150#endif /* TURING_SELECT_H */