interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / node / infer.om

609 lines · 13019 bytes Raw Blame History
1note Omega Node inference adapter (DRA-290): one bounded completion from the
2note own model's ollm serve (aien-train, DRA-119) over loopback, turned into a
3note versioned typed record. Composition, OM-2:
4note { printf 'om2\n'; cat seed/json/json.om seed/json/jsonw.om
5note seed/net/http.om seed/net/notls.om seed/node/infer.om; } | omc
6note Usage: omnode-infer PORT MAXTOK DEADLINE_S PROMPT [TESTOUT]
7note It connects only to 127.0.0.1 (offline-local: no other host, no TLS, no
8note credential) and refuses port 18091. POST /v1/completions with the prompt
9note as the whole template, greedy (top_p 0), seed 1, stream false. TESTOUT
10note is sent as ollm_test_output, which only a server started with test=1
11note honours; the Node's workflow never passes it.
12note The whole exchange (connect, request, generation, response) has one
13note deadline. A caller cancels by killing the process group; the server
14note sees the connection close and stops generating.
15note Record on stdout, exit 0: one line
16note OMNI1 kind fin ptok ctok plen olen nlen tlen
17note then the path, old, new and raw text bytes back to back. kind: 1
18note read_file, 2 omega_check, 3 replace_text, 4 the answer Tests passed.,
19note 5 the answer File not found., 9 the text is not a protocol action (the
20note model's turn is returned, never repaired). fin: 1 stop, 2 length, 0
21note other. The action is the text before the first newline-Done., trimmed,
22note as the round40 repair protocol reads it: a JSON object of exactly
23note {tool,path} for read_file and omega_check, or {tool,path,old,new} for
24note replace_text; path 1..128 bytes, old 1..40, new 0..40, no NUL byte.
25note Other exits: 2 usage, 3 prompt (1..3800 bytes, no NUL), 4 connect, 5
26note deadline, 6 HTTP status other than 200 (OMNE1 status, then the body
27note head, on stdout), 7 malformed response, 8 server busy (503) until less
28note than 2 s of the deadline is left: a busy server (one request at a time,
29note for example still stopping a cancelled one) is asked again every second.
30text ipath "/v1/completions"
31text imeth "POST"
32text ihost "Host"
33text ihostv "127.0.0.1"
34text ictype "Content-Type"
35text ijson "application/json"
36text kmodel "model"
37text vollm "ollm"
38text kprompt "prompt"
39text kmaxt "max_tokens"
40text ktopp "top_p"
41text kseed "seed"
42text kstream "stream"
43text ktout "ollm_test_output"
44text kobj "object"
45text vtcomp "text_completion"
46text kchoi "choices"
47text ktext "text"
48text kfin "finish_reason"
49text kusage "usage"
50text kpt "prompt_tokens"
51text kct "completion_tokens"
52text vstop "stop"
53text vlen "length"
54text ktool "tool"
55text kpath "path"
56text kold "old"
57text knew "new"
58text tread "read_file"
59text tcheck "omega_check"
60text trepl "replace_text"
61text apass "Tests passed."
62text amiss "File not found."
63text tdone "\nDone."
64text irec "OMNI1 "
65text ierr "OMNE1 "
66
67note The decimal value of a[0, n), or -1 when it is not 1..9 digits.
68fn ia_dec a view uchar * n ulong to long
69if or eq n 0 gt n 9
70ret neg long 1
71end
72v = ulong 0
73for i ulong n
74c = ulong get a i
75if or lt c 48 gt c 57
76ret neg long 1
77end
78v = add mul v 10 sub c 48
79end
80ret long v
81end
82
83note Argument i as a number, -1 when it is not one.
84fn ia_num i ulong to long
85b = new uchar 16 0
86n = argread i b 0 16
87if lt n 0
88ret neg long 1
89end
90ret ia_dec b ulong n
91end
92
93note Append the decimal v to o at p; the new end.
94fn ia_put o edit uchar 8192 p ulong v ulong to ulong
95d = new uchar 24 0
96k = ulong 0
97x = v
98for i ulong 24
99put d k uchar add 48 rem x 10
100k = add k 1
101x = div x 10
102if eq x 0
103break
104end
105end
106for j ulong k
107put o add p j get d sub sub k j 1
108end
109ret add p k
110end
111
112note Append a[0, n) to o at p; the new end.
113fn ia_cat o edit uchar 8192 p ulong a view uchar * s ulong n ulong to ulong
114for i ulong n
115put o add p i get a add s i
116end
117ret add p n
118end
119
120fn ia_out o view uchar 8192 n ulong to void
121p = ulong 0
122for it ulong 64
123if ge p n
124ret
125end
126r = write 1 o p sub n p
127if le r 0
128ret
129end
130p = add p ulong r
131end
132end
133
134note The request body into ob (jsonw); its length, or a negative refusal.
135fn ia_body ob edit uchar 16384 pr view uchar 4096 pn ulong mt ulong to long
136tb = new uchar 4096 0
137x = js_winit ob
138js_wobj ob
139js_wsbuf ob kmodel 0 len kmodel
140js_wsbuf ob vollm 0 len vollm
141js_wsbuf ob kprompt 0 len kprompt
142js_wsbuf ob pr 0 pn
143js_wsbuf ob kmaxt 0 len kmaxt
144js_wulong ob mt
145js_wsbuf ob ktopp 0 len ktopp
146js_wulong ob 0
147js_wsbuf ob kseed 0 len kseed
148js_wulong ob 1
149js_wsbuf ob kstream 0 len kstream
150js_wbool ob false
151if gt argcnt 5
152tn = argread 5 tb 0 4096
153if lt tn 0
154ret neg long 1
155end
156js_wsbuf ob ktout 0 len ktout
157js_wsbuf ob tb 0 ulong tn
158end
159js_wobje ob
160ret js_wdone ob
161end
162
163note Connect, send the request, read the status. The status, or negative.
164fn ia_send cs edit ulong 320 io edit uchar 131072 ob view uchar 16384 bn ulong port ulong dl long to long
165a = new uchar 16 0
166r = haddr4 a 2130706433 port
167fd = hdial a 5000000000
168if lt fd 0
169ret neg long 1000
170end
171r = hinit cs io int fd 0
172hsettmo cs dl dl
173hsetbdl cs dl
174r = hreqst cs io imeth
175r = hfe r hreqtg cs io ipath
176r = hfe r hhname cs io ihost
177r = hfe r hhval cs io ihostv
178r = hfe r hhname cs io ictype
179r = hfe r hhval cs io ijson
180r = hfe r hhend cs io 0 bn
181r = hfe r hput cs io view part ob 1040 bn
182r = hfe r hwfin cs io
183if lt r 0
184ret r
185end
186ret hrsprd cs io
187end
188
189note Member key in object o of the parsed src, its value token or jsnone.
190fn ia_mem src view uchar * tp view uint 65536 o ulong key view uchar * to ulong
191if ne js_type tp o 1
192ret jsnone
193end
194k = js_first tp o
195for it ulong js_nkids tp o
196if eq k jsnone
197break
198end
199if js_streq src js_off tp k key
200ret add k 1
201end
202k = js_mnext tp k
203end
204ret jsnone
205end
206
207note An unsigned member of object o, or -1.
208fn ia_umem src view uchar 16384 tp view uint 65536 o ulong key view uchar * to long
209v = ia_mem src tp o key
210if eq v jsnone
211ret neg long 1
212end
213if or ne js_type tp v 7 not js_isu src js_off tp v
214ret neg long 1
215end
216ret long js_tou src js_off tp v
217end
218
219note fin from token v: 1 stop, 2 length, 0 other.
220fn ia_fin src view uchar 16384 tp view uint 65536 v ulong to ulong
221if eq v jsnone
222ret 0
223end
224if ne js_type tp v 6
225ret 0
226end
227if js_streq src js_off tp v vstop
228ret 1
229end
230if js_streq src js_off tp v vlen
231ret 2
232end
233ret 0
234end
235
236note Usage of the parsed response into m[2] prompt and m[3] completion
237note tokens; false when either is missing.
238fn ia_use rb view uchar 16384 tp view uint 65536 m edit ulong 8 to bool
239us = ia_mem rb tp 0 kusage
240pt = ia_umem rb tp us kpt
241ct = ia_umem rb tp us kct
242if or lt pt 0 lt ct 0
243ret false
244end
245put m 2 ulong pt
246put m 3 ulong ct
247ret true
248end
249
250note The response body rb[0, rn): the text into tx, and m[0] text length,
251note m[1] fin, m[2] prompt tokens, m[3] completion tokens. 0 or 7.
252fn ia_resp rb view uchar 16384 rn ulong tx edit uchar 8192 m edit ulong 8 to ulong
253tp = new uint 65536 0
254x = js_init tp 16384 16
255r = js_parse rb tp rn
256if lt r 0
257ret 7
258end
259ob = ia_mem rb tp 0 kobj
260if or eq ob jsnone ne js_type tp ob 6
261ret 7
262end
263if not js_streq rb js_off tp ob vtcomp
264ret 7
265end
266ch = ia_mem rb tp 0 kchoi
267if or eq ch jsnone ne js_type tp ch 3
268ret 7
269end
270c0 = js_first tp ch
271if eq c0 jsnone
272ret 7
273end
274t = ia_mem rb tp c0 ktext
275if or eq t jsnone ne js_type tp t 6
276ret 7
277end
278n = js_sget rb js_off tp t tx
279if lt n 0
280ret 7
281end
282put m 0 ulong n
283put m 1 ia_fin rb tp ia_mem rb tp c0 kfin
284if not ia_use rb tp m
285ret 7
286end
287ret 0
288end
289
290note true when tx[i, n) starts with a newline then Done.
291fn ia_done tx view uchar 8192 i ulong n ulong to bool
292if gt add i 6 n
293ret false
294end
295for j ulong 6
296if ne get tx add i j get tdone j
297ret false
298end
299end
300ret true
301end
302
303note The action text of tx[0, n): before the first newline-Done., trimmed of
304note ASCII white space. Its start in m[4] and length in m[5].
305fn ia_trim tx view uchar 8192 n ulong m edit ulong 8 to void
306e = n
307for i ulong n
308if ia_done tx i n
309e = i
310break
311end
312end
313s = ulong 0
314for j ulong e
315if gt get tx s 32
316break
317end
318s = add s 1
319end
320for k ulong sub e s
321if gt get tx sub e 1 32
322break
323end
324e = sub e 1
325end
326put m 4 s
327put m 5 sub e s
328end
329
330note true when tx[s, s + n) equals text t.
331fn ia_is tx view uchar 8192 s ulong n ulong t view uchar * to bool
332if ne n len t
333ret false
334end
335for i ulong n
336if ne get tx add s i get t i
337ret false
338end
339end
340ret true
341end
342
343note Field f of the action object o (0 path at d[0], at most 128 bytes; 1
344note old at d[128], 40; 2 new at d[192], 40), a string decoded into d with
345note no NUL byte. Its length, or -1.
346fn ia_sfld src view uchar 8192 tp view uint 65536 o ulong f ulong d edit uchar 8192 to long
347v = ulong jsnone
348fo = ulong 0
349mx = ulong 128
350if eq f 0
351v = ia_mem src tp o kpath
352end
353if eq f 1
354v = ia_mem src tp o kold
355fo = 128
356mx = 40
357end
358if eq f 2
359v = ia_mem src tp o knew
360fo = 192
361mx = 40
362end
363if eq v jsnone
364ret neg long 1
365end
366if ne js_type tp v 6
367ret neg long 1
368end
369n = js_sget src js_off tp v part d fo mx
370if lt n 0
371ret neg long 1
372end
373for i ulong ulong n
374if eq get d add fo i 0
375ret neg long 1
376end
377end
378ret n
379end
380
381note Classify the action at a[0, an): kind (1..3, 9), the fields into d at
382note 0 (path), 128 (old), 192 (new) with lengths in m[1..3].
383fn ia_json a view uchar 8192 an ulong d edit uchar 8192 m edit ulong 8 to ulong
384tp = new uint 65536 0
385x = js_init tp 8192 4
386r = js_parse a tp an
387if lt r 0
388ret 9
389end
390if ne js_type tp 0 1
391ret 9
392end
393tv = ia_mem a tp 0 ktool
394if or eq tv jsnone ne js_type tp tv 6
395ret 9
396end
397k = ulong 9
398nk = ulong 2
399if js_streq a js_off tp tv tread
400k = 1
401end
402if js_streq a js_off tp tv tcheck
403k = 2
404end
405if js_streq a js_off tp tv trepl
406k = 3
407nk = 4
408end
409if or eq k 9 ne js_nkids tp 0 nk
410ret 9
411end
412pl = ia_sfld a tp 0 0 d
413if lt pl 1
414ret 9
415end
416put m 1 ulong pl
417if ne k 3
418ret k
419end
420ol = ia_sfld a tp 0 1 d
421nl = ia_sfld a tp 0 2 d
422if or lt ol 1 lt nl 0
423ret 9
424end
425put m 2 ulong ol
426put m 3 ulong nl
427ret 3
428end
429
430note Write the record for text tx[0, m0) with response fields m to stdout.
431fn ia_emit tx view uchar 8192 m edit ulong 8 to void
432o = new uchar 8192 0
433d = new uchar 8192 0
434a = new uchar 8192 0
435ia_trim tx get m 0 m
436s = get m 4
437n = get m 5
438for i ulong n
439put a i get tx add s i
440end
441ac = new ulong 8 0
442k = ulong 9
443if ia_is tx s n apass
444k = 4
445end
446if ia_is tx s n amiss
447k = 5
448end
449if eq k 9
450k = ia_json a n d ac
451end
452p = ia_cat o 0 irec 0 len irec
453p = ia_put o p k
454put o p 32
455p = ia_put o add p 1 get m 1
456put o p 32
457p = ia_put o add p 1 get m 2
458put o p 32
459p = ia_put o add p 1 get m 3
460put o p 32
461p = ia_put o add p 1 get ac 1
462put o p 32
463p = ia_put o add p 1 get ac 2
464put o p 32
465p = ia_put o add p 1 get ac 3
466put o p 32
467p = ia_put o add p 1 get m 0
468put o p 10
469p = ia_cat o add p 1 d 0 get ac 1
470p = ia_cat o p d 128 get ac 2
471p = ia_cat o p d 192 get ac 3
472p = ia_cat o p tx 0 get m 0
473ia_out o p
474end
475
476note An HTTP error: OMNE1 status, then up to 512 bytes of the body.
477fn ia_herr cs edit ulong 320 io edit uchar 131072 st long to int
478o = new uchar 8192 0
479b = new uchar 512 0
480n = hbodyrd cs io b
481p = ia_cat o 0 ierr 0 len ierr
482p = ia_put o p ulong st
483put o p 10
484p = add p 1
485if gt n 0
486p = ia_cat o p b 0 ulong n
487end
488ia_out o p
489x = hclose cs
490if eq st 503
491ret 8
492end
493ret 6
494end
495
496note The prompt (argument 4) into pr; its length, or -1 when it is empty,
497note over 3800 bytes or holds a NUL byte.
498fn ia_prompt pr edit uchar 4096 to long
499n = argread 4 pr 0 3800
500if lt n 1
501ret neg long 1
502end
503for i ulong ulong n
504if eq get pr i 0
505ret neg long 1
506end
507end
508ret n
509end
510
511note Arguments PORT MAXTOK DEADLINE_S into a[0..2]; false when one is out of
512note range (port 1..65535 but never 18091, 1..256 tokens, 1..3600 s).
513fn ia_args a edit ulong 4 to bool
514if or lt argcnt 5 gt argcnt 6
515ret false
516end
517port = ia_num 1
518mt = ia_num 2
519ds = ia_num 3
520if or or lt port 1 gt port 65535 eq port 18091
521ret false
522end
523if or lt mt 1 gt mt 256
524ret false
525end
526if or lt ds 1 gt ds 3600
527ret false
528end
529put a 0 ulong port
530put a 1 ulong mt
531put a 2 ulong ds
532ret true
533end
534
535note After the status st: the record, or the exit code of a failure.
536fn ia_recv cs edit ulong 320 io edit uchar 131072 st long to int
537rb = new uchar 16384 0
538tx = new uchar 8192 0
539m = new ulong 8 0
540if eq st neg long 1000
541ret 4
542end
543if eq st HETIME
544x = hclose cs
545ret 5
546end
547if lt st 0
548x = hclose cs
549ret 4
550end
551if ne st 200
552ret ia_herr cs io st
553end
554rn = hbodyall cs io rb
555x = hclose cs
556if eq rn HETIME
557ret 5
558end
559if lt rn 0
560ret 7
561end
562e = ia_resp rb ulong rn tx m
563if ne e 0
564ret 7
565end
566ia_emit tx m
567ret 0
568end
569
570fn ia_main to int
571cs = new ulong 320 0
572io = new uchar 131072 0
573pr = new uchar 4096 0
574ob = new uchar 16384 0
575a = new ulong 4 0
576if not ia_args a
577ret 2
578end
579pn = ia_prompt pr
580if lt pn 1
581ret 3
582end
583bn = ia_body ob pr ulong pn get a 1
584if lt bn 0
585ret 3
586end
587ret ia_ask cs io ob ulong bn get a 0 long mul get a 2 1000000000
588end
589
590note The exchange within dl ns from now; a 503 is asked again every second
591note while at least 2 s are left. The record, or the exit code of a failure.
592fn ia_ask cs edit ulong 320 io edit uchar 131072 ob view uchar 16384 bn ulong po ulong dl long to int
593t0 = monons
594st = ia_send cs io ob bn po dl
595for it ulong 3600
596left = sub dl long sub monons t0
597if or ne st 503 lt left 2000000000
598ret ia_recv cs io st
599end
600x = hclose cs
601r = sleepns 1000000000
602st = ia_send cs io ob bn po sub left 1000000000
603end
604ret ia_recv cs io st
605end
606
607fn main to int
608ret ia_main
609end