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 / tools / estimation / est_replay.h
| 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 | /* EST-2/EST-3 replay library (protocol v1 and v2, docs/estimation/EST23_PROTOCOL_V1.md, EST23_PROTOCOL_V2.md). |
| 18 | * Shared parsing, SHA-256 file verification, and one strictly causal forward |
| 19 | * pass over a machine-state.ndjson file with model M0 or M1. Tools only: it |
| 20 | * may open files and print. The filter itself is src/estimation/est_kf.c. */ |
| 21 | #ifndef OMEGA_EST_REPLAY_H |
| 22 | #define OMEGA_EST_REPLAY_H |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "est_kf.h" |
| 29 | #include "est_mix.h" |
| 30 | #include "est_types.h" |
| 31 | |
| 32 | #define EST_PROTOCOL_COMMIT "f96dc97" |
| 33 | #define EST_RUN_A_ID "20260929T020536Z-ad8e1f2ea4e4-silicon" |
| 34 | #define EST_RUN_B_ID "3e9e53be3358" |
| 35 | #define EST_RUN_A_SHA "6df4a2b079482b79c42da84da5627b6a8fb9c7d3ce885aac53437b0199d517db" |
| 36 | #define EST_BURN_IN 30u |
| 37 | #define EST_NQ 19 |
| 38 | #define EST_NR 25 |
| 39 | #define EST_PROTOCOL_DOC_SHA "dbd2a407a8cf12d353f9abff5d26985fb2f9d96f6d33872896169082c6082676" |
| 40 | #define EST_SOURCE_TEXT "R15.machine-state.thermal_mc[0]" |
| 41 | |
| 42 | /* Run B identity (from that run's SHA256SUMS text only; no data file is read to know these). */ |
| 43 | #define EST_RUN_B_RAW_SHA_DEFAULT "91a5fe34225926cd7aca2fca4be5ed51dc0772341a2fda5f9a39d0f72b8a6bb5" |
| 44 | #define EST_RUN_B_MARKS_SHA_DEFAULT "d2dcc89fb1f9bfff15e1dce6be5248ba6f285cfe1fb7dd051c09764aec844d04" |
| 45 | const char *est_run_b_id(void); |
| 46 | const char *est_run_b_raw_sha(void); |
| 47 | const char *est_run_b_marks_sha(void); |
| 48 | /* Nonzero only when est_eval --recorded has proven both inputs are run B. */ |
| 49 | extern int est_allow_run_b; |
| 50 | /* 1 when the resolved (realpath) path has run B's id in any component. */ |
| 51 | int est_path_is_run_b(const char *path); |
| 52 | /* Find "<hex> <name>" in dir/SHA256SUMS (text only). 0 when found. */ |
| 53 | int est_sums_lookup(const char *dir, const char *name, char hex[65]); |
| 54 | |
| 55 | /* Whole file in memory, verified against the SHA256SUMS line beside it. */ |
| 56 | typedef struct { |
| 57 | char path[1024]; |
| 58 | uint8_t *data; |
| 59 | size_t len; |
| 60 | size_t nlines; |
| 61 | size_t *off; |
| 62 | size_t *llen; |
| 63 | char sha_hex[65]; |
| 64 | } est_file; |
| 65 | |
| 66 | /* Loads path and refuses (returns nonzero, message in err) when the file's |
| 67 | * SHA-256 differs from its line in SHA256SUMS in the same directory, or when |
| 68 | * there is no such line. */ |
| 69 | int est_file_load(const char *path, est_file *f, char *err, size_t errcap); |
| 70 | /* Same reader without SHA256SUMS or the run B guard (used for temp copies). */ |
| 71 | int est_file_load_raw(const char *path, est_file *f, char *err, size_t errcap); |
| 72 | void est_file_free(est_file *f); |
| 73 | /* Hex SHA-256 of a whole file on disk (0 on success). */ |
| 74 | int est_sha_file_hex(const char *path, char out[65]); |
| 75 | void est_hex(const uint8_t *d, size_t n, char *out); |
| 76 | |
| 77 | /* Parsers. Return 0 on success, nonzero when the line does not parse. */ |
| 78 | int est_parse_t(const char *line, size_t len, int64_t *t_ns); |
| 79 | int est_parse_thermal0(const char *line, size_t len, double *value); |
| 80 | /* "<sec>.<9 digits>" at the start of s (used for marks lines). Returns 2 when |
| 81 | * the seconds field has more than 10 digits or would overflow int64 ns. */ |
| 82 | int est_parse_time_prefix(const char *s, size_t len, int64_t *t_ns, size_t *used); |
| 83 | |
| 84 | double est_grid_q(int i); |
| 85 | double est_grid_r(int j); |
| 86 | |
| 87 | int est_make_model(int model_id, double q, double r, est_model *out); |
| 88 | |
| 89 | typedef struct { |
| 90 | size_t line; /* absolute 0-based line index in the file */ |
| 91 | size_t L; /* 0-based index from the first valid line */ |
| 92 | int prior; /* 1 for the declared prior step */ |
| 93 | int coast; /* 1 when the observation was missing */ |
| 94 | int t_ok; /* raw t parsed */ |
| 95 | int chain_ok; /* obs -> prediction -> innovation chain verified */ |
| 96 | int64_t wall_ns; /* raw t (integer ns), 0 when !t_ok */ |
| 97 | int64_t t_ns; /* logical grid time */ |
| 98 | uint32_t horizon; |
| 99 | double z, y_mean, S, nu, nis; |
| 100 | est_digest evidence, obs_d, pred_d, innov_d, belief_d; |
| 101 | est_belief post; |
| 102 | } est_step; |
| 103 | |
| 104 | typedef struct { |
| 105 | est_step *steps; |
| 106 | size_t nsteps, cap; |
| 107 | size_t lines_used; /* lines consumed (limit applied) */ |
| 108 | size_t leading_missing; /* lines before the first valid observation */ |
| 109 | size_t coasts; /* missing observations after the first valid */ |
| 110 | size_t bad_value, bad_t; /* diagnostics, over all lines used */ |
| 111 | size_t bad_t_overflow; /* t with too many seconds digits (subset of bad_t) */ |
| 112 | size_t gap_zero, gap_backward; /* duplicate / backward t between parsed lines */ |
| 113 | size_t multi_horizon; /* steps with horizon > 1 */ |
| 114 | est_model model; |
| 115 | est_digest model_d; |
| 116 | int error; /* nonzero: filter refused something */ |
| 117 | char errmsg[160]; |
| 118 | } est_replay; |
| 119 | |
| 120 | /* One causal pass over the first `limit` lines (SIZE_MAX = all). Reuses |
| 121 | * out->steps storage when out was used before (zero-initialise once). */ |
| 122 | int est_replay_run(const est_file *f, size_t limit, int model_id, double q, double r, |
| 123 | est_replay *out); |
| 124 | void est_replay_free(est_replay *rp); |
| 125 | void est_replay_write_stream(FILE *fp, const est_replay *rp, int model_id); |
| 126 | |
| 127 | /* Fit selection: best finite log-likelihood, strict >, q outer / r inner, so a |
| 128 | * tie keeps the smaller q then the smaller r. Returns 1 when none is finite. */ |
| 129 | int est_fit_pick(const double ll[EST_NQ][EST_NR], int *bi, int *bj); |
| 130 | |
| 131 | /* Parameter file (text). */ |
| 132 | typedef struct { |
| 133 | char fit_path[1024]; |
| 134 | char fit_sha[65]; |
| 135 | size_t fit_lines; |
| 136 | double q[2], r[2], ll[2]; |
| 137 | } est_params; |
| 138 | int est_params_write(const char *path, const est_params *p); |
| 139 | int est_params_read(const char *path, est_params *p); |
| 140 | /* Protocol checks: q and r finite and on the 19x25 grid (1e-9 relative). */ |
| 141 | int est_params_validate(const est_params *p, char *err, size_t cap); |
| 142 | |
| 143 | |
| 144 | /* ---- Protocol v2 (docs/estimation/EST23_PROTOCOL_V2.md) ---- |
| 145 | * M2: M0's structure with q = total variance of a fitted noise shape and |
| 146 | * r = EST_M2_R, so the predicted mean is the previous observation exactly |
| 147 | * (persistence) and the predicted change has the shape in est_mix.h. |
| 148 | * Fit run C1 and held-out run C2 are fresh 1 Hz collections (EST-3b). */ |
| 149 | #define EST_V2_FIT_TAG "-est3b-fit-" |
| 150 | #define EST_V2_HELDOUT_TAG "-est3b-heldout-" |
| 151 | #define EST_M2_R 1e-12 |
| 152 | #define EST_M2_FLOOR (100.0 * 100.0 / 12.0) /* 100 mC sensor step: uniform quantization variance */ |
| 153 | #define EST_M2_K 3u |
| 154 | #define EST_M2_ITERS 1000u |
| 155 | /* C1 identity, fixed when C1 was committed (SHA256SUMS line beside it). */ |
| 156 | #define EST_V2_FIT_SHA "65252bae5f9d49d30a3b334fd2b9444c36db7627a45fcd9b6ef0bb4e5a7e5716" |
| 157 | /* C2 identity (SHA256SUMS text beside it) and the frozen v2 protocol document. */ |
| 158 | #define EST_V2_HELDOUT_RAW_SHA "pending" |
| 159 | #define EST_V2_HELDOUT_MARKS_SHA "pending" |
| 160 | #define EST_V2_PROTOCOL_DOC_SHA "86b7b46f02dd6e51db78cc9f450aac27be189cdb1484894e923a1898429536f5" |
| 161 | #define EST_V2_PROTOCOL_DOC "docs/estimation/EST23_PROTOCOL_V2.md" |
| 162 | /* Nonzero only when est_eval --protocol-v2 --recorded has proven the inputs are C2. */ |
| 163 | extern int est_allow_heldout; |
| 164 | /* 1 when the path or its resolved path names the held-out run. */ |
| 165 | int est_path_is_heldout(const char *path); |
| 166 | |
| 167 | typedef struct { |
| 168 | char v1_params_path[1024]; |
| 169 | char v1_params_sha[65]; |
| 170 | char fit_path[1024]; |
| 171 | char fit_sha[65]; |
| 172 | size_t fit_lines; |
| 173 | size_t fit_n; /* one-step changes used by the EM fit */ |
| 174 | double q, r, ll; /* M2 q (= total shape variance), r, EM log-likelihood */ |
| 175 | est_mix mix; |
| 176 | } est_params2; |
| 177 | int est_params2_write(const char *path, const est_params2 *p); |
| 178 | /* Exactly the format written by est_params2_write, nothing else. */ |
| 179 | int est_params2_read(const char *path, est_params2 *p); |
| 180 | /* M2 fit data: one-step changes (horizon 1, observed, L >= burn-in) of a |
| 181 | * replay with model 2. Fills e (cap entries) and returns the count; returns |
| 182 | * (size_t)-1 when the replay mean is not exactly the previous observation. */ |
| 183 | size_t est_m2_changes(const est_replay *rp, double *e, size_t cap); |
| 184 | |
| 185 | #endif |