interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / om / hive-task / task.om

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