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 / seed / authority / action-gate.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note OM-CONTROL-659 action contract v1. See ../README.md for the trusted-caller boundary. |
| 3 | note No I/O, authority minting or persistence. Caller serializes and durably commits transitions. |
| 4 | note Record words 0..7: version,state,attempt,limit,generation,epoch,expiry,reserved. |
| 5 | note Words 8..31: operation,request,scope,provider,policy,grant identities, four words each. |
| 6 | note State 1 ready,2 in-flight,3 unknown,4 performed. No timeout implies not performed. |
| 7 | |
| 8 | fn actvalid r view ulong 32 to ulong |
| 9 | if ne get r 0 1 |
| 10 | ret 11 |
| 11 | end |
| 12 | if or lt get r 1 1 gt get r 1 4 |
| 13 | ret 12 |
| 14 | end |
| 15 | if or eq get r 3 0 gt get r 3 1024 |
| 16 | ret 12 |
| 17 | end |
| 18 | if gt get r 2 get r 3 |
| 19 | ret 12 |
| 20 | end |
| 21 | if and ne get r 1 1 eq get r 2 0 |
| 22 | ret 12 |
| 23 | end |
| 24 | if or eq get r 4 0 eq get r 5 0 |
| 25 | ret 12 |
| 26 | end |
| 27 | if or eq get r 6 0 ne get r 7 0 |
| 28 | ret 12 |
| 29 | end |
| 30 | for g ulong 6 |
| 31 | bits = ulong 0 |
| 32 | for j ulong 4 |
| 33 | bits = or bits get r add 8 add mul g 4 j |
| 34 | end |
| 35 | if eq bits 0 |
| 36 | ret 12 |
| 37 | end |
| 38 | end |
| 39 | ret 0 |
| 40 | end |
| 41 | |
| 42 | fn actbind r view ulong 32 c view ulong 32 to ulong |
| 43 | for g ulong 6 |
| 44 | diff = ulong 0 |
| 45 | for j ulong 4 |
| 46 | ix = add 8 add mul g 4 j |
| 47 | diff = or diff xor get r ix get c ix |
| 48 | end |
| 49 | if ne diff 0 |
| 50 | ret add 40 g |
| 51 | end |
| 52 | end |
| 53 | ret 0 |
| 54 | end |
| 55 | |
| 56 | fn actauth r view ulong 32 c view ulong 32 to ulong |
| 57 | if ne get c 0 1 |
| 58 | ret 11 |
| 59 | end |
| 60 | if or ne get c 3 0 or ne get c 6 0 ne get c 7 0 |
| 61 | ret 12 |
| 62 | end |
| 63 | if ne get c 1 1 |
| 64 | ret 30 |
| 65 | end |
| 66 | if ne get r 4 get c 4 |
| 67 | ret 31 |
| 68 | end |
| 69 | if ne get r 5 get c 5 |
| 70 | ret 32 |
| 71 | end |
| 72 | if ge get c 2 get r 6 |
| 73 | ret 33 |
| 74 | end |
| 75 | ret actbind r c |
| 76 | end |
| 77 | |
| 78 | fn actstart r edit ulong 32 c view ulong 32 to ulong |
| 79 | err = actvalid r |
| 80 | if ne err 0 |
| 81 | ret err |
| 82 | end |
| 83 | if eq get r 1 3 |
| 84 | ret 21 |
| 85 | end |
| 86 | if eq get r 1 4 |
| 87 | ret 22 |
| 88 | end |
| 89 | if ne get r 1 1 |
| 90 | ret 20 |
| 91 | end |
| 92 | if ge get r 2 get r 3 |
| 93 | ret 23 |
| 94 | end |
| 95 | err = actauth r c |
| 96 | if ne err 0 |
| 97 | ret err |
| 98 | end |
| 99 | put r 2 add get r 2 1 |
| 100 | put r 1 2 |
| 101 | ret 0 |
| 102 | end |
| 103 | |
| 104 | fn actlost r edit ulong 32 to ulong |
| 105 | err = actvalid r |
| 106 | if ne err 0 |
| 107 | ret err |
| 108 | end |
| 109 | if eq get r 1 3 |
| 110 | ret 0 |
| 111 | end |
| 112 | if ne get r 1 2 |
| 113 | ret 20 |
| 114 | end |
| 115 | put r 1 3 |
| 116 | ret 0 |
| 117 | end |
| 118 | |
| 119 | note A trusted receipt maps outcome 0 unknown,1 definitely not performed,2 performed. |
| 120 | note Not-found or timeout is outcome 0 unless the provider proves absence and quiescence. |
| 121 | note Receipt words 2/3: exact attempt and verification=1. Words 6/7 must be zero. |
| 122 | note Recording an observed outcome is permitted after expiry; another attempt is not. |
| 123 | fn actrecon r edit ulong 32 s view ulong 32 to ulong |
| 124 | err = actvalid r |
| 125 | if ne err 0 |
| 126 | ret err |
| 127 | end |
| 128 | if not or eq get r 1 2 eq get r 1 3 |
| 129 | ret 20 |
| 130 | end |
| 131 | if ne get s 0 1 |
| 132 | ret 11 |
| 133 | end |
| 134 | if or gt get s 1 2 or ne get s 6 0 ne get s 7 0 |
| 135 | ret 12 |
| 136 | end |
| 137 | if ne get s 3 1 |
| 138 | ret 36 |
| 139 | end |
| 140 | if ne get s 2 get r 2 |
| 141 | ret 35 |
| 142 | end |
| 143 | if ne get s 4 get r 4 |
| 144 | ret 31 |
| 145 | end |
| 146 | if ne get s 5 get r 5 |
| 147 | ret 32 |
| 148 | end |
| 149 | err = actbind r s |
| 150 | if ne err 0 |
| 151 | ret err |
| 152 | end |
| 153 | if eq get s 1 0 |
| 154 | put r 1 3 |
| 155 | ret 21 |
| 156 | end |
| 157 | if eq get s 1 1 |
| 158 | put r 1 1 |
| 159 | else |
| 160 | put r 1 4 |
| 161 | end |
| 162 | ret 0 |
| 163 | end |
| 164 | note SPDX-License-Identifier: AGPL-3.0-only |
| 165 | note Tests call the production transition functions. Provider/store I/O is not simulated as durable. |
| 166 | global checks ulong 1 |
| 167 | global fails ulong 1 |
| 168 | global report uchar 64 |
| 169 | text title "action checks=" |
| 170 | |
| 171 | fn check got ulong want ulong to void |
| 172 | put checks 0 add get checks 0 1 |
| 173 | if ne got want |
| 174 | put fails 0 add get fails 0 1 |
| 175 | end |
| 176 | end |
| 177 | |
| 178 | fn plan r edit ulong 32 to void |
| 179 | for i ulong 32 |
| 180 | put r i 0 |
| 181 | end |
| 182 | put r 0 1 |
| 183 | put r 1 1 |
| 184 | put r 3 3 |
| 185 | put r 4 7 |
| 186 | put r 5 9 |
| 187 | put r 6 100 |
| 188 | for i ulong 24 |
| 189 | put r add i 8 add i 100 |
| 190 | end |
| 191 | end |
| 192 | |
| 193 | fn context c edit ulong 32 to void |
| 194 | for i ulong 32 |
| 195 | put c i 0 |
| 196 | end |
| 197 | put c 0 1 |
| 198 | put c 1 1 |
| 199 | put c 2 50 |
| 200 | put c 4 7 |
| 201 | put c 5 9 |
| 202 | for i ulong 24 |
| 203 | put c add i 8 add i 100 |
| 204 | end |
| 205 | end |
| 206 | |
| 207 | fn receipt s edit ulong 32 n ulong outcome ulong to void |
| 208 | context s |
| 209 | put s 1 outcome |
| 210 | put s 2 n |
| 211 | put s 3 1 |
| 212 | end |
| 213 | |
| 214 | fn summary to int |
| 215 | for i ulong len title |
| 216 | put report i get title i |
| 217 | end |
| 218 | n = get checks 0 |
| 219 | at = len title |
| 220 | put report at uchar add 48 div n 100 |
| 221 | put report add at 1 uchar add 48 rem div n 10 10 |
| 222 | put report add at 2 uchar add 48 rem n 10 |
| 223 | put report add at 3 32 |
| 224 | put report add at 4 uchar add 48 div get fails 0 100 |
| 225 | put report add at 5 uchar add 48 rem div get fails 0 10 10 |
| 226 | put report add at 6 uchar add 48 rem get fails 0 10 |
| 227 | put report add at 7 10 |
| 228 | w = write 1 report 0 add at 8 |
| 229 | if ne w long add at 8 |
| 230 | ret 2 |
| 231 | end |
| 232 | if ne get fails 0 0 |
| 233 | ret 1 |
| 234 | end |
| 235 | ret 0 |
| 236 | end |
| 237 | |
| 238 | fn main to int |
| 239 | r = new ulong 32 0 |
| 240 | c = new ulong 32 0 |
| 241 | s = new ulong 32 0 |
| 242 | plan r |
| 243 | context c |
| 244 | check actstart r c 0 |
| 245 | check get r 1 2 |
| 246 | check get r 2 1 |
| 247 | check actstart r c 20 |
| 248 | check get r 2 1 |
| 249 | check actlost r 0 |
| 250 | check get r 1 3 |
| 251 | check actlost r 0 |
| 252 | check actstart r c 21 |
| 253 | receipt s 1 0 |
| 254 | check actrecon r s 21 |
| 255 | check get r 1 3 |
| 256 | check actstart r c 21 |
| 257 | receipt s 1 2 |
| 258 | check actrecon r s 0 |
| 259 | check get r 1 4 |
| 260 | check actstart r c 22 |
| 261 | check actrecon r s 20 |
| 262 | |
| 263 | note An observed uncertain outcome also moves an in-flight record to unknown. |
| 264 | plan r |
| 265 | check actstart r c 0 |
| 266 | receipt s 1 0 |
| 267 | check actrecon r s 21 |
| 268 | check get r 1 3 |
| 269 | check actstart r c 21 |
| 270 | |
| 271 | note Confirmed not performed enables a new attempt only after a fresh admission check. |
| 272 | plan r |
| 273 | check actstart r c 0 |
| 274 | check actlost r 0 |
| 275 | receipt s 1 1 |
| 276 | check actrecon r s 0 |
| 277 | check get r 1 1 |
| 278 | check get r 2 1 |
| 279 | put c 1 0 |
| 280 | check actstart r c 30 |
| 281 | check get r 2 1 |
| 282 | context c |
| 283 | put c 4 8 |
| 284 | check actstart r c 31 |
| 285 | context c |
| 286 | put c 5 10 |
| 287 | check actstart r c 32 |
| 288 | context c |
| 289 | put c 2 100 |
| 290 | check actstart r c 33 |
| 291 | context c |
| 292 | check actstart r c 0 |
| 293 | check get r 2 2 |
| 294 | receipt s 1 2 |
| 295 | check actrecon r s 35 |
| 296 | check get r 1 2 |
| 297 | receipt s 2 2 |
| 298 | check actrecon r s 0 |
| 299 | check actstart r c 22 |
| 300 | |
| 301 | note Every word of every identity is checked before mutating the record. |
| 302 | for i ulong 24 |
| 303 | plan r |
| 304 | context c |
| 305 | put c add i 8 add get c add i 8 1 |
| 306 | check actstart r c add 40 div i 4 |
| 307 | check get r 1 1 |
| 308 | check get r 2 0 |
| 309 | end |
| 310 | note Receipt binding includes all identities and cannot resolve a different attempt. |
| 311 | for i ulong 24 |
| 312 | plan r |
| 313 | context c |
| 314 | check actstart r c 0 |
| 315 | receipt s 1 1 |
| 316 | put s add i 8 add get s add i 8 1 |
| 317 | check actrecon r s add 40 div i 4 |
| 318 | check get r 1 2 |
| 319 | end |
| 320 | |
| 321 | plan r |
| 322 | context c |
| 323 | put r 2 3 |
| 324 | check actstart r c 23 |
| 325 | put r 2 18446744073709551615 |
| 326 | check actstart r c 12 |
| 327 | check get r 2 18446744073709551615 |
| 328 | plan r |
| 329 | put r 3 0 |
| 330 | check actstart r c 12 |
| 331 | plan r |
| 332 | put r 3 1025 |
| 333 | check actstart r c 12 |
| 334 | plan r |
| 335 | put r 7 1 |
| 336 | check actstart r c 12 |
| 337 | plan r |
| 338 | put r 0 2 |
| 339 | check actstart r c 11 |
| 340 | plan r |
| 341 | put r 1 99 |
| 342 | check actstart r c 12 |
| 343 | plan r |
| 344 | put r 1 2 |
| 345 | check actstart r c 12 |
| 346 | plan r |
| 347 | context c |
| 348 | put c 0 2 |
| 349 | check actstart r c 11 |
| 350 | context c |
| 351 | put c 1 2 |
| 352 | check actstart r c 30 |
| 353 | context c |
| 354 | put c 3 1 |
| 355 | check actstart r c 12 |
| 356 | context c |
| 357 | for group ulong 6 |
| 358 | plan r |
| 359 | for j ulong 4 |
| 360 | put r add 8 add mul group 4 j 0 |
| 361 | end |
| 362 | check actstart r c 12 |
| 363 | end |
| 364 | |
| 365 | plan r |
| 366 | check actstart r c 0 |
| 367 | receipt s 1 1 |
| 368 | put s 3 0 |
| 369 | check actrecon r s 36 |
| 370 | check get r 1 2 |
| 371 | receipt s 1 1 |
| 372 | put s 4 8 |
| 373 | check actrecon r s 31 |
| 374 | receipt s 1 1 |
| 375 | put s 5 10 |
| 376 | check actrecon r s 32 |
| 377 | receipt s 1 3 |
| 378 | check actrecon r s 12 |
| 379 | receipt s 1 1 |
| 380 | put s 6 1 |
| 381 | check actrecon r s 12 |
| 382 | check actlost r 0 |
| 383 | check actstart r c 21 |
| 384 | receipt s 1 1 |
| 385 | check actrecon r s 0 |
| 386 | check actlost r 20 |
| 387 | check get r 1 1 |
| 388 | |
| 389 | note Limit is enforced across reconciliation cycles, without rotating operation identity. |
| 390 | plan r |
| 391 | for i ulong 3 |
| 392 | check actstart r c 0 |
| 393 | receipt s add i 1 1 |
| 394 | check actrecon r s 0 |
| 395 | end |
| 396 | check actstart r c 23 |
| 397 | check get r 2 3 |
| 398 | check get r 8 100 |
| 399 | ret summary |
| 400 | end |