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 / tests / polyglot / polyglot_explain.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note tests/polyglot/polyglot_explain.om -- pure seeded OM-2 POLYGLOT-0 deterministic |
| 5 | note receipt explainer (spec/polyglot-0.md section 9). |
| 6 | note Reads ONLY the contract file (--spec) and receipt directory (--receipts), |
| 7 | note sorts .json filenames in deterministic byte order, verifies contract digest, |
| 8 | note and prints deterministic byte-identical explanation output. |
| 9 | |
| 10 | global g_pe_arg uchar 512 |
| 11 | global g_pe_spec uchar 512 |
| 12 | global g_pe_dir uchar 512 |
| 13 | global g_pe_run uchar 128 |
| 14 | global g_pe_contract uchar 64 |
| 15 | note g_pe_cfg: [0]=spec_len [1]=dir_len [2]=run_len [3]=nfiles [4]=nvalid [5]=nign [6]=nstale [7]=nsmoke |
| 16 | global g_pe_cfg ulong 8 |
| 17 | |
| 18 | global g_pe_dbuf uchar 65536 |
| 19 | note Up to 256 receipt filenames, 128 bytes each |
| 20 | global g_pe_names uchar 32768 |
| 21 | global g_pe_nlens ulong 256 |
| 22 | global g_pe_order ulong 256 |
| 23 | global g_pe_path uchar 768 |
| 24 | |
| 25 | text t_pe_sp_flag "--spec" |
| 26 | text t_pe_rc_flag "--receipts" |
| 27 | text t_pe_rn_flag "--run" |
| 28 | text t_pe_usage "usage: polyglot_explain --spec spec/polyglot-0.md --receipts DIR [--run RUN_ID]\n" |
| 29 | text t_pe_schema "OMEGA_POLYGLOT0_RECEIPT_V1" |
| 30 | text t_pe_hdr "POLYGLOT-0 explanation (spec/polyglot-0.md section 9, pure OM-2)\ncontract section 1 sha256: " |
| 31 | text t_pe_rec1 "\nreceipts: " |
| 32 | text t_pe_rec2 " read, " |
| 33 | text t_pe_rec3 " ignored (not OMEGA_POLYGLOT0_RECEIPT_V1), " |
| 34 | text t_pe_rec4 " stale contract, " |
| 35 | text t_pe_rec5 " smoke\n" |
| 36 | text t_pe_file " receipt: " |
| 37 | text t_pe_ok " [contract=MATCH" |
| 38 | text t_pe_stale " [contract=STALE" |
| 39 | text t_pe_smk " smoke=true]\n" |
| 40 | text t_pe_nsmk " smoke=false]\n" |
| 41 | text t_pe_ksmoke "\"smoke\": true" |
| 42 | |
| 43 | fn pe_arg_eq alen ulong b view uchar * to bool |
| 44 | if ne alen len b |
| 45 | ret false |
| 46 | end |
| 47 | for i ulong alen bound 512 |
| 48 | if ne get g_pe_arg i get b i |
| 49 | ret false |
| 50 | end |
| 51 | end |
| 52 | ret true |
| 53 | end |
| 54 | |
| 55 | fn pe_dbuf_ends_json off ulong n ulong to bool |
| 56 | if le n 5 |
| 57 | ret false |
| 58 | end |
| 59 | b = add off sub n 5 |
| 60 | if ne get g_pe_dbuf b 46 |
| 61 | ret false |
| 62 | end |
| 63 | if ne get g_pe_dbuf add b 1 106 |
| 64 | ret false |
| 65 | end |
| 66 | if ne get g_pe_dbuf add b 2 115 |
| 67 | ret false |
| 68 | end |
| 69 | if ne get g_pe_dbuf add b 3 111 |
| 70 | ret false |
| 71 | end |
| 72 | if ne get g_pe_dbuf add b 4 110 |
| 73 | ret false |
| 74 | end |
| 75 | ret true |
| 76 | end |
| 77 | |
| 78 | fn pe_fbuf_contains pat view uchar * flen ulong to bool |
| 79 | plen = len pat |
| 80 | if gt plen flen |
| 81 | ret false |
| 82 | end |
| 83 | lim = add sub flen plen 1 |
| 84 | for i ulong lim bound 262144 |
| 85 | ok = ulong 1 |
| 86 | for j ulong plen bound 128 |
| 87 | if ne get g_omx_fbuf add i j get pat j |
| 88 | ok = 0 |
| 89 | break |
| 90 | end |
| 91 | end |
| 92 | if eq ok 1 |
| 93 | ret true |
| 94 | end |
| 95 | end |
| 96 | ret false |
| 97 | end |
| 98 | |
| 99 | fn pe_fbuf_contains_contract flen ulong to bool |
| 100 | if lt flen 64 |
| 101 | ret false |
| 102 | end |
| 103 | lim = add sub flen 64 1 |
| 104 | for i ulong lim bound 262144 |
| 105 | ok = ulong 1 |
| 106 | for j ulong 64 |
| 107 | if ne get g_omx_fbuf add i j get g_pe_contract j |
| 108 | ok = 0 |
| 109 | break |
| 110 | end |
| 111 | end |
| 112 | if eq ok 1 |
| 113 | ret true |
| 114 | end |
| 115 | end |
| 116 | ret false |
| 117 | end |
| 118 | |
| 119 | fn pe_cmp_names ia ulong ib ulong to long |
| 120 | la = get g_pe_nlens ia |
| 121 | lb = get g_pe_nlens ib |
| 122 | mn = la |
| 123 | if lt lb mn |
| 124 | mn = lb |
| 125 | end |
| 126 | ba = mul ia 128 |
| 127 | bb = mul ib 128 |
| 128 | for i ulong mn bound 128 |
| 129 | ca = ulong get g_pe_names add ba i |
| 130 | cb = ulong get g_pe_names add bb i |
| 131 | if lt ca cb |
| 132 | ret -1 |
| 133 | end |
| 134 | if gt ca cb |
| 135 | ret 1 |
| 136 | end |
| 137 | end |
| 138 | if lt la lb |
| 139 | ret -1 |
| 140 | end |
| 141 | if gt la lb |
| 142 | ret 1 |
| 143 | end |
| 144 | ret 0 |
| 145 | end |
| 146 | |
| 147 | fn pe_sort_names n ulong to void |
| 148 | for i ulong n bound 256 |
| 149 | put g_pe_order i i |
| 150 | end |
| 151 | for i ulong n bound 256 |
| 152 | j = add i 1 |
| 153 | while lt j n bound 256 |
| 154 | oi = get g_pe_order i |
| 155 | oj = get g_pe_order j |
| 156 | if gt pe_cmp_names oi oj 0 |
| 157 | put g_pe_order i oj |
| 158 | put g_pe_order j oi |
| 159 | end |
| 160 | j = add j 1 |
| 161 | end |
| 162 | end |
| 163 | end |
| 164 | |
| 165 | fn pe_add_dent noff ulong nlen ulong to void |
| 166 | if not pe_dbuf_ends_json noff nlen |
| 167 | ret |
| 168 | end |
| 169 | if ge nlen 128 |
| 170 | ret |
| 171 | end |
| 172 | nf = get g_pe_cfg 3 |
| 173 | if ge nf 256 |
| 174 | ret |
| 175 | end |
| 176 | dst = mul nf 128 |
| 177 | for k ulong nlen bound 128 |
| 178 | put g_pe_names add dst k get g_pe_dbuf add noff k |
| 179 | end |
| 180 | put g_pe_nlens nf nlen |
| 181 | put g_pe_cfg 3 add nf 1 |
| 182 | end |
| 183 | |
| 184 | fn pe_scan_buf nu ulong to void |
| 185 | pos = ulong 0 |
| 186 | while lt pos nu bound 65536 |
| 187 | reclen = or ulong get g_pe_dbuf add pos 16 shl ulong get g_pe_dbuf add pos 17 8 |
| 188 | if lt reclen 20 |
| 189 | break |
| 190 | end |
| 191 | noff = add pos 19 |
| 192 | nlen = ulong 0 |
| 193 | lim = add pos reclen |
| 194 | while lt add noff nlen lim bound 256 |
| 195 | if eq get g_pe_dbuf add noff nlen 0 |
| 196 | break |
| 197 | end |
| 198 | nlen = add nlen 1 |
| 199 | end |
| 200 | pe_add_dent noff nlen |
| 201 | pos = add pos reclen |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | fn pe_scan_dir to int |
| 206 | fd = fopen g_pe_dir 0 get g_pe_cfg 1 0 0 |
| 207 | if lt fd 0 |
| 208 | ret -1 |
| 209 | end |
| 210 | fdi = int fd |
| 211 | while true bound 1024 |
| 212 | nread = getdnts fdi g_pe_dbuf 0 65536 |
| 213 | if le nread 0 |
| 214 | break |
| 215 | end |
| 216 | pe_scan_buf ulong nread |
| 217 | end |
| 218 | _c = fclose fdi |
| 219 | ret 0 |
| 220 | end |
| 221 | |
| 222 | fn pe_build_path idx ulong to ulong |
| 223 | dlen = get g_pe_cfg 1 |
| 224 | p = ulong 0 |
| 225 | for i ulong dlen bound 512 |
| 226 | put g_omx_path p get g_pe_dir i |
| 227 | p = add p 1 |
| 228 | end |
| 229 | put g_omx_path p uchar 47 |
| 230 | p = add p 1 |
| 231 | nlen = get g_pe_nlens idx |
| 232 | base = mul idx 128 |
| 233 | for i ulong nlen bound 128 |
| 234 | put g_omx_path p get g_pe_names add base i |
| 235 | p = add p 1 |
| 236 | end |
| 237 | ret p |
| 238 | end |
| 239 | |
| 240 | fn pe_process_one idx ulong to void |
| 241 | plen = pe_build_path idx |
| 242 | flen = omx_read_gpath plen |
| 243 | if lt flen 0 |
| 244 | put g_pe_cfg 5 add get g_pe_cfg 5 1 |
| 245 | ret |
| 246 | end |
| 247 | fu = ulong flen |
| 248 | if not pe_fbuf_contains t_pe_schema fu |
| 249 | put g_pe_cfg 5 add get g_pe_cfg 5 1 |
| 250 | ret |
| 251 | end |
| 252 | put g_pe_cfg 4 add get g_pe_cfg 4 1 |
| 253 | cmatch = pe_fbuf_contains_contract fu |
| 254 | if not cmatch |
| 255 | put g_pe_cfg 6 add get g_pe_cfg 6 1 |
| 256 | end |
| 257 | ismoke = pe_fbuf_contains t_pe_ksmoke fu |
| 258 | if ismoke |
| 259 | put g_pe_cfg 7 add get g_pe_cfg 7 1 |
| 260 | end |
| 261 | omx_puts t_pe_file |
| 262 | _w = write 1 g_pe_names mul idx 128 get g_pe_nlens idx |
| 263 | if cmatch |
| 264 | omx_puts t_pe_ok |
| 265 | else |
| 266 | omx_puts t_pe_stale |
| 267 | end |
| 268 | if ismoke |
| 269 | omx_puts t_pe_smk |
| 270 | else |
| 271 | omx_puts t_pe_nsmk |
| 272 | end |
| 273 | end |
| 274 | |
| 275 | fn main to int |
| 276 | for i ulong 8 |
| 277 | put g_pe_cfg i 0 |
| 278 | end |
| 279 | argc = ulong argcnt |
| 280 | i = ulong 1 |
| 281 | while lt i argc bound 32 |
| 282 | alen = argread i g_pe_arg 0 512 |
| 283 | if le alen 0 |
| 284 | break |
| 285 | end |
| 286 | au = ulong alen |
| 287 | if pe_arg_eq au t_pe_sp_flag |
| 288 | i = add i 1 |
| 289 | if lt i argc |
| 290 | sl = argread i g_omx_path 0 512 |
| 291 | if gt sl 0 |
| 292 | put g_pe_cfg 0 ulong sl |
| 293 | end |
| 294 | end |
| 295 | else |
| 296 | if pe_arg_eq au t_pe_rc_flag |
| 297 | i = add i 1 |
| 298 | if lt i argc |
| 299 | dl = argread i g_pe_dir 0 512 |
| 300 | if gt dl 0 |
| 301 | put g_pe_cfg 1 ulong dl |
| 302 | end |
| 303 | end |
| 304 | else |
| 305 | if pe_arg_eq au t_pe_rn_flag |
| 306 | i = add i 1 |
| 307 | if lt i argc |
| 308 | rl = argread i g_pe_run 0 128 |
| 309 | if gt rl 0 |
| 310 | put g_pe_cfg 2 ulong rl |
| 311 | end |
| 312 | end |
| 313 | end |
| 314 | end |
| 315 | end |
| 316 | i = add i 1 |
| 317 | end |
| 318 | if or eq get g_pe_cfg 0 0 eq get g_pe_cfg 1 0 |
| 319 | omx_eputs t_pe_usage |
| 320 | ret 2 |
| 321 | end |
| 322 | if ne omx_contract_digest_gpath get g_pe_cfg 0 0 |
| 323 | ret 2 |
| 324 | end |
| 325 | for k ulong 64 |
| 326 | put g_pe_contract k get g_omx_shhex k |
| 327 | end |
| 328 | if ne pe_scan_dir 0 |
| 329 | ret 2 |
| 330 | end |
| 331 | nf = get g_pe_cfg 3 |
| 332 | pe_sort_names nf |
| 333 | omx_puts t_pe_hdr |
| 334 | _w = write 1 g_pe_contract 0 64 |
| 335 | omx_nl |
| 336 | for k ulong nf bound 256 |
| 337 | pe_process_one get g_pe_order k |
| 338 | end |
| 339 | omx_puts t_pe_rec1 |
| 340 | omx_putu get g_pe_cfg 4 |
| 341 | omx_puts t_pe_rec2 |
| 342 | omx_putu get g_pe_cfg 5 |
| 343 | omx_puts t_pe_rec3 |
| 344 | omx_putu get g_pe_cfg 6 |
| 345 | omx_puts t_pe_rec4 |
| 346 | omx_putu get g_pe_cfg 7 |
| 347 | omx_puts t_pe_rec5 |
| 348 | ret 0 |
| 349 | end |