aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / hive-task / task.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note hive-task: the pure transition rules of the hive forge task authority (issue #925, DRA-288). |
| 3 | note This file is the source of truth. crates/spark-hive/src/store.rs is the named host adapter: |
| 4 | note each mutation is one conditional UPDATE whose WHERE clause states these rules, stepped to |
| 5 | note completion so a failed commit is reported, and a command record is written in the same |
| 6 | note IMMEDIATE transaction as the change it answers. drive.om in this directory checks the hand |
| 7 | note cases, then drives the real spark-cockpit-rs binary over HTTP, compares every transition of |
| 8 | note the table with this program, and shows mutants are caught. |
| 9 | note |
| 10 | note Input on stdin, ASCII decimal numbers separated by spaces, tabs or newlines, ten per case: |
| 11 | note op st gen rgen amatch art verdict now exp ttl |
| 12 | note op: 1 claim, 2 renew (heartbeat), 3 submit, 4 verify, 5 reap (expired lease back to open), |
| 13 | note 6 classify a database failure (st holds the SQLite primary result code), 7 command id. |
| 14 | note st: current state, 0 open, 1 claimed, 2 submitted, 3 verified, 4 failed; any other value is an |
| 15 | note unknown state and every operation refuses it. |
| 16 | note gen: current generation (attempt number); rgen: generation named by the request. |
| 17 | note amatch: 1 when the request names the current attempt token and holder, else 0. |
| 18 | note art: 1 when verify names the exact artifact recorded at submit, else 0. |
| 19 | note verdict: verify outcome, 1 pass, 0 fail. |
| 20 | note now, exp: milliseconds since 1970; the lease is live while now < exp. |
| 21 | note ttl: requested lease length in seconds (claim and renew). |
| 22 | note Output: one line per case, "dec next_state next_gen". |
| 23 | note dec: 0 allow, 2 bad state, 3 held by a live lease, 4 stale attempt, 5 lease expired, |
| 24 | note 6 generation exhausted, 7 artifact or generation mismatch, 8 ttl out of range, |
| 25 | note 9 database busy or locked: nothing changed and the request may be retried, |
| 26 | note 10 any other database failure: not retryable. |
| 27 | note (1 is task not found, decided by the adapter when no row exists.) |
| 28 | note The adapter answers 2..7 with HTTP 409 refused and 8 with 400 invalid: it checks the ttl |
| 29 | note before it reads the database, and its WHERE clause repeats the bound. |
| 30 | note On a refusal next_state = st and next_gen = gen. Only an allowed claim changes the generation, |
| 31 | note to gen + 1, and a claim is refused once gen reaches GMAX, so generations never wrap. |
| 32 | note |
| 33 | note Op 7 decides a write that may carry a caller-chosen command id, so a retried request is |
| 34 | note answered once. Its fields reuse the ten slots: |
| 35 | note 7 prior idlen bad dig has 0 0 0 sdec |
| 36 | note has: 1 when the request carries a command id (an empty one counts), else 0. |
| 37 | note idlen: id length in bytes; bad: id bytes outside A-Z a-z 0-9 . _ : - |
| 38 | note prior: 1 when a record already holds this id (looked up only for a well-formed id). |
| 39 | note dig: 1 when that record's request digest equals this request's digest. The digest covers the |
| 40 | note operation, the task id and every normalized request field. |
| 41 | note sdec: the decision of ops 1..6 for the request itself (0..10, 1 task not found). |
| 42 | note Output "dec rec chg". dec: 0 proceed with the operation (its answer is sdec), 11 replay: answer |
| 43 | note with the recorded result, 12 command conflict: the id was used for another request, |
| 44 | note 13 bad command id (must be 1 to CMDMAX bytes of the set above). |
| 45 | note rec: 1 when the id holds a record after the request. Only an allowed outcome is recorded, so a |
| 46 | note refusal, a busy database or a failure is decided again when retried with the same id. |
| 47 | note chg: 1 when the operation's transition is applied; a replay or a conflict changes nothing. |
| 48 | note |
| 49 | note Exit: 0 ok, 2 read error, 3 input over 16 MiB, 5 bad or missing number, 6 field out of range, |
| 50 | note 9 write error. |
| 51 | const OPEN ulong 0 |
| 52 | const CLAIMED ulong 1 |
| 53 | const SUBMTD ulong 2 |
| 54 | const VERIFD ulong 3 |
| 55 | const FAILED ulong 4 |
| 56 | const OCLAIM ulong 1 |
| 57 | const ORENEW ulong 2 |
| 58 | const OSUBMIT ulong 3 |
| 59 | const OVERIFY ulong 4 |
| 60 | const OREAP ulong 5 |
| 61 | const ODBERR ulong 6 |
| 62 | const OCMD ulong 7 |
| 63 | const ALLOW ulong 0 |
| 64 | const BADSTATE ulong 2 |
| 65 | const HELD ulong 3 |
| 66 | const STALE ulong 4 |
| 67 | const EXPIRED ulong 5 |
| 68 | const EXHAUST ulong 6 |
| 69 | const MISMATCH ulong 7 |
| 70 | const BADTTL ulong 8 |
| 71 | const RETRY ulong 9 |
| 72 | const DBFAIL ulong 10 |
| 73 | const REPLAY ulong 11 |
| 74 | const CONFLICT ulong 12 |
| 75 | const BADCMD ulong 13 |
| 76 | note CMDMAX: longest command id in bytes. |
| 77 | const CMDMAX ulong 128 |
| 78 | note SQLite primary result codes SQLITE_BUSY and SQLITE_LOCKED. |
| 79 | const SQBUSY ulong 5 |
| 80 | const SQLOCK ulong 6 |
| 81 | note GMAX = 2^53 - 1, the largest generation a JSON client reads exactly. |
| 82 | const GMAX ulong 9007199254740991 |
| 83 | const MAXTTL ulong 86400 |
| 84 | global inb uchar 16777216 |
| 85 | note hd: 0 parse position, 1 input length. |
| 86 | global hd ulong 2 |
| 87 | note cs: the current case, fields in input order. |
| 88 | global cs ulong 10 |
| 89 | global ob uchar 65536 |
| 90 | global ow ulong 1 |
| 91 | |
| 92 | note live: the lease is unexpired at now. |
| 93 | fn live now ulong exp ulong to bool |
| 94 | ret lt now exp |
| 95 | end |
| 96 | |
| 97 | note ttlok: a lease length the authority grants, 1 second to 1 day. |
| 98 | fn ttlok ttl ulong to bool |
| 99 | ret and ge ttl 1 le ttl MAXTTL |
| 100 | end |
| 101 | |
| 102 | note fenced: the request names the current attempt, by generation and attempt token. |
| 103 | fn fenced gen ulong rgen ulong am ulong to bool |
| 104 | ret and eq rgen gen eq am 1 |
| 105 | end |
| 106 | |
| 107 | note dclaim: claim from open or failed, or take over a claimed task whose lease has expired. |
| 108 | fn dclaim st ulong gen ulong now ulong exp ulong ttl ulong to ulong |
| 109 | if not ttlok ttl |
| 110 | ret BADTTL |
| 111 | end |
| 112 | el = or eq st OPEN eq st FAILED |
| 113 | if eq st CLAIMED |
| 114 | if live now exp |
| 115 | ret HELD |
| 116 | end |
| 117 | el = true |
| 118 | end |
| 119 | if not el |
| 120 | ret BADSTATE |
| 121 | end |
| 122 | if ge gen GMAX |
| 123 | ret EXHAUST |
| 124 | end |
| 125 | ret ALLOW |
| 126 | end |
| 127 | |
| 128 | note dlease: renew and submit act only for the current attempt of a claimed task, before expiry. |
| 129 | fn dlease st ulong fen bool now ulong exp ulong to ulong |
| 130 | if ne st CLAIMED |
| 131 | ret BADSTATE |
| 132 | end |
| 133 | if not fen |
| 134 | ret STALE |
| 135 | end |
| 136 | if not live now exp |
| 137 | ret EXPIRED |
| 138 | end |
| 139 | ret ALLOW |
| 140 | end |
| 141 | |
| 142 | note drenew: dlease plus a lease length in range. |
| 143 | fn drenew st ulong fen bool now ulong exp ulong ttl ulong to ulong |
| 144 | if not ttlok ttl |
| 145 | ret BADTTL |
| 146 | end |
| 147 | ret dlease st fen now exp |
| 148 | end |
| 149 | |
| 150 | note dverify: verify only a submitted task, naming its generation and exact artifact. |
| 151 | fn dverify st ulong gen ulong rgen ulong art ulong to ulong |
| 152 | if ne st SUBMTD |
| 153 | ret BADSTATE |
| 154 | end |
| 155 | if or ne rgen gen ne art 1 |
| 156 | ret MISMATCH |
| 157 | end |
| 158 | ret ALLOW |
| 159 | end |
| 160 | |
| 161 | note dreap: only a claimed task whose lease has expired goes back to open. |
| 162 | fn dreap st ulong now ulong exp ulong to ulong |
| 163 | if ne st CLAIMED |
| 164 | ret BADSTATE |
| 165 | end |
| 166 | if live now exp |
| 167 | ret HELD |
| 168 | end |
| 169 | ret ALLOW |
| 170 | end |
| 171 | |
| 172 | fn nextst op ulong st ulong vd ulong dec ulong to ulong |
| 173 | if ne dec ALLOW |
| 174 | ret st |
| 175 | end |
| 176 | if or eq op OCLAIM eq op ORENEW |
| 177 | ret CLAIMED |
| 178 | end |
| 179 | if eq op OSUBMIT |
| 180 | ret SUBMTD |
| 181 | end |
| 182 | if eq op OVERIFY |
| 183 | if eq vd 1 |
| 184 | ret VERIFD |
| 185 | end |
| 186 | ret FAILED |
| 187 | end |
| 188 | ret OPEN |
| 189 | end |
| 190 | |
| 191 | fn nextgen op ulong gen ulong dec ulong to ulong |
| 192 | if and eq dec ALLOW eq op OCLAIM |
| 193 | ret add gen 1 |
| 194 | end |
| 195 | ret gen |
| 196 | end |
| 197 | |
| 198 | note ddberr: SQLite busy or locked means the statement changed nothing and may be retried. |
| 199 | fn ddberr code ulong to ulong |
| 200 | if or eq code SQBUSY eq code SQLOCK |
| 201 | ret RETRY |
| 202 | end |
| 203 | ret DBFAIL |
| 204 | end |
| 205 | |
| 206 | note dcmd: the command id rule. No id: proceed as before. A malformed id is refused before the |
| 207 | note database is opened. A ttl out of range is bad input, answered before the database is opened. |
| 208 | note A busy or failed database answers before the record can be read, so its answer is the |
| 209 | note failure and nothing changes. A new id proceeds; a known id replays its record for the same |
| 210 | note request and is a conflict for any other request. |
| 211 | note Fields of cs under op 7: 1 prior, 2 idlen, 3 bad, 4 dig, 5 has, 9 sdec. |
| 212 | fn dcmd to ulong |
| 213 | has = get cs 5 |
| 214 | idl = get cs 2 |
| 215 | bad = get cs 3 |
| 216 | prior = get cs 1 |
| 217 | dig = get cs 4 |
| 218 | sdec = get cs 9 |
| 219 | if ne has 1 |
| 220 | ret ALLOW |
| 221 | end |
| 222 | if or eq idl 0 gt idl CMDMAX |
| 223 | ret BADCMD |
| 224 | end |
| 225 | if ne bad 0 |
| 226 | ret BADCMD |
| 227 | end |
| 228 | if eq sdec BADTTL |
| 229 | ret ALLOW |
| 230 | end |
| 231 | if ge sdec RETRY |
| 232 | ret ALLOW |
| 233 | end |
| 234 | if eq prior 0 |
| 235 | ret ALLOW |
| 236 | end |
| 237 | if eq dig 1 |
| 238 | ret REPLAY |
| 239 | end |
| 240 | ret CONFLICT |
| 241 | end |
| 242 | |
| 243 | note crec: does the id hold a record after the request? A replay or a conflict keeps the earlier |
| 244 | note record; a new id is recorded only when the operation itself is allowed, and a refusal or a |
| 245 | note database failure leaves the records as they were. |
| 246 | fn crec has ulong dc ulong sdec ulong prior ulong to ulong |
| 247 | if or eq dc REPLAY eq dc CONFLICT |
| 248 | ret 1 |
| 249 | end |
| 250 | if ne dc ALLOW |
| 251 | ret 0 |
| 252 | end |
| 253 | if ne has 1 |
| 254 | ret 0 |
| 255 | end |
| 256 | if ne sdec ALLOW |
| 257 | ret prior |
| 258 | end |
| 259 | ret 1 |
| 260 | end |
| 261 | |
| 262 | note cchg: the operation's transition runs only when the command proceeds and it is allowed. |
| 263 | fn cchg dc ulong sdec ulong to ulong |
| 264 | if and eq dc ALLOW eq sdec ALLOW |
| 265 | ret 1 |
| 266 | end |
| 267 | ret 0 |
| 268 | end |
| 269 | |
| 270 | note decide: the decision for the case in cs (op already checked to be 1..7). |
| 271 | fn decide to ulong |
| 272 | op = get cs 0 |
| 273 | st = get cs 1 |
| 274 | gen = get cs 2 |
| 275 | now = get cs 7 |
| 276 | exp = get cs 8 |
| 277 | if eq op OCLAIM |
| 278 | ret dclaim st gen now exp get cs 9 |
| 279 | end |
| 280 | if eq op OREAP |
| 281 | ret dreap st now exp |
| 282 | end |
| 283 | if eq op ODBERR |
| 284 | ret ddberr st |
| 285 | end |
| 286 | if eq op OCMD |
| 287 | ret dcmd |
| 288 | end |
| 289 | if eq op OVERIFY |
| 290 | ret dverify st gen get cs 3 get cs 5 |
| 291 | end |
| 292 | fen = fenced gen get cs 3 get cs 4 |
| 293 | if eq op ORENEW |
| 294 | ret drenew st fen now exp get cs 9 |
| 295 | end |
| 296 | ret dlease st fen now exp |
| 297 | end |
| 298 | |
| 299 | note valid: fields in range; st and the times may hold any value except under op 7, where prior |
| 300 | note is 0 or 1 and sdec a decision of ops 1..6. |
| 301 | fn valid to bool |
| 302 | op = get cs 0 |
| 303 | ok = and ge op OCLAIM le op OCMD |
| 304 | ok = and ok and le get cs 2 GMAX le get cs 3 GMAX |
| 305 | ok = and ok and le get cs 4 1 le get cs 5 1 |
| 306 | ok = and ok le get cs 6 1 |
| 307 | if eq op OCMD |
| 308 | ok = and ok and le get cs 1 1 le get cs 9 DBFAIL |
| 309 | end |
| 310 | ret ok |
| 311 | end |
| 312 | |
| 313 | note second, third: the last two output fields, next_state next_gen, or rec chg under op 7. |
| 314 | fn second dc ulong to ulong |
| 315 | op = get cs 0 |
| 316 | if eq op OCMD |
| 317 | ret crec get cs 5 dc get cs 9 get cs 1 |
| 318 | end |
| 319 | ret nextst op get cs 1 get cs 6 dc |
| 320 | end |
| 321 | |
| 322 | fn third dc ulong to ulong |
| 323 | if eq get cs 0 OCMD |
| 324 | ret cchg dc get cs 9 |
| 325 | end |
| 326 | ret nextgen get cs 0 get cs 2 dc |
| 327 | end |
| 328 | |
| 329 | note slurp: read all of stdin into inb. Each inner pass adds at least one byte or ends, so |
| 330 | note 64 outer passes of 999999 cover the 16777216-byte buffer. |
| 331 | fn slurp to int |
| 332 | n = ulong 0 |
| 333 | for r ulong 64 |
| 334 | while lt n len inb bound 999999 |
| 335 | k = read 0 inb n sub len inb n |
| 336 | if lt k 0 |
| 337 | ret 2 |
| 338 | end |
| 339 | if eq k 0 |
| 340 | put hd 1 n |
| 341 | ret 0 |
| 342 | end |
| 343 | n = add n ulong k |
| 344 | end |
| 345 | end |
| 346 | ret 3 |
| 347 | end |
| 348 | |
| 349 | fn isws c uchar to bool |
| 350 | ret or or eq c 32 eq c 10 or eq c 9 eq c 13 |
| 351 | end |
| 352 | |
| 353 | note skip: advance the parse position past white space; return the new position. 17 passes of |
| 354 | note 999999 bytes cover the 16777216-byte buffer. |
| 355 | fn skip to ulong |
| 356 | p = get hd 0 |
| 357 | n = get hd 1 |
| 358 | for r ulong 17 |
| 359 | k = ulong 0 |
| 360 | while and lt p n lt k 999999 bound 999999 |
| 361 | if not isws get inb p |
| 362 | put hd 0 p |
| 363 | ret p |
| 364 | end |
| 365 | p = add p 1 |
| 366 | k = add k 1 |
| 367 | end |
| 368 | end |
| 369 | put hd 0 p |
| 370 | ret p |
| 371 | end |
| 372 | |
| 373 | note num: next decimal number of at most 19 digits (below 2^64, so no wrap); exit 5 otherwise. |
| 374 | fn num to ulong |
| 375 | p = skip |
| 376 | n = get hd 1 |
| 377 | v = ulong 0 |
| 378 | d = ulong 0 |
| 379 | while lt p n bound 20 |
| 380 | c = get inb p |
| 381 | if or lt c 48 gt c 57 |
| 382 | break |
| 383 | end |
| 384 | if eq d 19 |
| 385 | exit 5 |
| 386 | end |
| 387 | v = add mul v 10 ulong sub c 48 |
| 388 | p = add p 1 |
| 389 | d = add d 1 |
| 390 | end |
| 391 | if eq d 0 |
| 392 | exit 5 |
| 393 | end |
| 394 | if lt p n |
| 395 | if not isws get inb p |
| 396 | exit 5 |
| 397 | end |
| 398 | end |
| 399 | put hd 0 p |
| 400 | ret v |
| 401 | end |
| 402 | |
| 403 | fn flush to int |
| 404 | o = ulong 0 |
| 405 | w = get ow 0 |
| 406 | while lt o w bound 65537 |
| 407 | k = write 1 ob o sub w o |
| 408 | if le k 0 |
| 409 | ret 9 |
| 410 | end |
| 411 | o = add o ulong k |
| 412 | end |
| 413 | put ow 0 0 |
| 414 | ret 0 |
| 415 | end |
| 416 | |
| 417 | fn badd c uchar to void |
| 418 | w = get ow 0 |
| 419 | if ge w len ob |
| 420 | exit 9 |
| 421 | end |
| 422 | put ob w c |
| 423 | put ow 0 add w 1 |
| 424 | end |
| 425 | |
| 426 | fn putd v ulong to void |
| 427 | p = ulong 1 |
| 428 | while le p div v 10 bound 20 |
| 429 | p = mul p 10 |
| 430 | end |
| 431 | while ne p 0 bound 21 |
| 432 | badd uchar add div v p 48 |
| 433 | v = rem v p |
| 434 | p = div p 10 |
| 435 | end |
| 436 | end |
| 437 | |
| 438 | fn main to int |
| 439 | r = slurp |
| 440 | if ne r 0 |
| 441 | ret r |
| 442 | end |
| 443 | note every case takes at least 20 bytes, so a 16 MiB input holds fewer than 838861 cases. |
| 444 | more = true |
| 445 | while more bound 999999 |
| 446 | p = skip |
| 447 | if ge p get hd 1 |
| 448 | more = false |
| 449 | else |
| 450 | for i ulong 10 |
| 451 | put cs i num |
| 452 | end |
| 453 | if not valid |
| 454 | ret 6 |
| 455 | end |
| 456 | dc = decide |
| 457 | putd dc |
| 458 | badd uchar 32 |
| 459 | putd second dc |
| 460 | badd uchar 32 |
| 461 | putd third dc |
| 462 | badd uchar 10 |
| 463 | if gt get ow 0 65000 |
| 464 | r = flush |
| 465 | if ne r 0 |
| 466 | ret r |
| 467 | end |
| 468 | end |
| 469 | end |
| 470 | end |
| 471 | ret flush |
| 472 | end |