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 / serve / main.om

388 lines · 8235 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/sovereign-core/om/serve/main.om
3note Standalone pure-Omega Sovereign Core host runtime and CLI executable (Linux AArch64).
4note Replaces the Rust aien-cli / aien-serve-http host front-end with a fail-closed seeded OM-2 binary.
5note Compose with:
6note si/directives/om2.om core/sovereign-core/om/scheduler/kv_cache.om
7note core/sovereign-core/om/scheduler/scheduler.om core/sovereign-core/om/serve/http_auth.om
8note core/sovereign-core/om/serve/router.om core/sovereign-core/om/serve/cli.om
9note om/tls/sha2.om core/sovereign-core/om/store/closure.om
10note core/sovereign-core/om/serve/main.om
11note Subcommands:
12note version print pure-Omega runtime version
13note health emit /health JSON status
14note models verify AIEN_SERVE_HTTP_BEARER_TOKEN env and list models
15note chat --model M [--max-tokens N] validate CLI, run scheduler+KV admission, emit completion JSON
16note serve --port P [--once] bind 127.0.0.1:P HTTP/1.1 server (requires AIEN_SERVE_HTTP_BEARER_TOKEN)
17note serve-stdio process one HTTP/1.1 request from stdin to stdout (requires bearer env)
18
19text sc_ver "aien-sovereign-core-om 1.0 (pure-omega host runtime/cli)\n"
20text sc_usage "usage: aien-om version|health|models|chat|serve|serve-stdio [flags]\n"
21text sc_err_env "ERROR: AIEN_SERVE_HTTP_BEARER_TOKEN missing or invalid (16..64 printable ASCII required)\n"
22text sc_err_cli "ERROR: invalid CLI arguments code="
23text sc_err_sock "ERROR: failed to bind loopback TCP socket\n"
24text sc_env_tok "AIEN_SERVE_HTTP_BEARER_TOKEN="
25text sc_cmd_stdio "serve-stdio"
26text sc_flag_once "--once"
27text sc_nl "\n"
28text sc_req_health "GET /health HTTP/1.1\nHost: localhost\n\n"
29text sc_req_models_a "GET /v1/models HTTP/1.1\nHost: localhost\nAuthorization: Bearer "
30text sc_req_models_b "\n\n"
31text sc_chat_out0 "{\"object\":\"chat.completion\",\"engine\":\"omega-sovereign-core\",\"admitted\":true,\"max_tokens\":"
32text sc_chat_out1 ",\"kv_free_blocks\":"
33text sc_chat_out2 "}\n"
34
35global sc_cmdline uchar 512
36global sc_argbuf uchar 128
37global sc_envbuf uchar 256
38global sc_rawreq uchar 4096
39global sc_sockbuf uchar 4096
40
41fn sc_w fd int s view uchar * to void
42n = len s
43off = ulong 0
44while lt off n bound 4096
45k = write fd s off sub n off
46if le k 0
47break
48end
49off = add off ulong k
50end
51end
52
53fn sc_w_u64 fd int v ulong to void
54buf = new uchar 24 0
55if eq v 0
56put buf 0 uchar 48
57sc_w fd part buf 0 1
58ret
59end
60tmp = new uchar 24 0
61cnt = ulong 0
62x = v
63while gt x 0 bound 24
64put tmp cnt uchar add 48 rem x 10
65cnt = add cnt 1
66x = div x 10
67end
68for i ulong cnt bound 24
69put buf i get tmp sub sub cnt 1 i
70end
71sc_w fd part buf 0 cnt
72end
73
74fn sc_w_resp fd int to void
75n = srv_resp_len
76off = ulong 0
77while lt off n bound 4096
78k = write fd srv_resp_buf off sub n off
79if le k 0
80break
81end
82off = add off ulong k
83end
84end
85
86note Load AIEN_SERVE_HTTP_BEARER_TOKEN from process environment (zero disk secrets).
87fn sc_load_env_bearer to bool
88tok = new uchar 64 0
89srv_auth_reset
90ec = envcnt
91if le ec 0
92ret false
93end
94plen = len sc_env_tok
95i = ulong 0
96uec = ulong ec
97while lt i uec bound 512
98n = envread i sc_envbuf 0 256
99if gt n long plen
100un = ulong n
101same = true
102for j ulong plen bound 64
103if ne get sc_envbuf j get sc_env_tok j
104same = false
105break
106end
107end
108if same
109tlen = sub un plen
110if or lt tlen SRV_MIN_TOKEN_LEN gt tlen SRV_MAX_TOKEN_LEN
111ret false
112end
113for j ulong tlen bound 64
114put tok j get sc_envbuf add plen j
115end
116ret srv_auth_set_token part tok 0 tlen
117end
118end
119i = add i 1
120end
121ret false
122end
123
124fn sc_arg_eq idx ulong lit view uchar * to bool
125n = argread idx sc_argbuf 0 128
126if lt n 0
127ret false
128end
129un = ulong n
130if ne un len lit
131ret false
132end
133for i ulong un bound 128
134if ne get sc_argbuf i get lit i
135ret false
136end
137end
138ret true
139end
140
141note Build space-separated command line from argv[1..argc) (skipping --once) into sc_cmdline.
142fn sc_build_cmdline argc ulong once_flag edit ulong 1 to ulong
143put once_flag 0 0
144w = ulong 0
145idx = ulong 1
146while lt idx argc bound 16
147if sc_arg_eq idx sc_flag_once
148put once_flag 0 1
149idx = add idx 1
150continue
151end
152n = argread idx sc_argbuf 0 128
153if le n 0
154idx = add idx 1
155continue
156end
157un = ulong n
158if gt w 0
159if lt w 512
160put sc_cmdline w uchar 32
161w = add w 1
162end
163end
164for j ulong un bound 128
165if lt w 512
166put sc_cmdline w get sc_argbuf j
167w = add w 1
168end
169end
170idx = add idx 1
171end
172ret w
173end
174
175note Expand LF -> CRLF from src into dst, returning written byte count.
176fn sc_expand_crlf src view uchar * dst edit uchar 512 to ulong
177n = len src
178w = ulong 0
179for i ulong n bound 512
180b = get src i
181if eq b uchar 10
182if lt add w 1 512
183put dst w uchar 13
184put dst add w 1 uchar 10
185w = add w 2
186end
187else
188if lt w 512
189put dst w b
190w = add w 1
191end
192end
193end
194ret w
195end
196
197fn sc_run_serve_stdio to int
198if not sc_load_env_bearer
199sc_w 2 sc_err_env
200ret 1
201end
202tot = ulong 0
203while lt tot SRV_MAX_REQ_BYTES bound 4096
204k = read 0 sc_rawreq tot sub SRV_MAX_REQ_BYTES tot
205if le k 0
206break
207end
208tot = add tot ulong k
209end
210req = new uchar 4096 0
211for i ulong tot bound 4096
212put req i get sc_rawreq i
213end
214st = srv_handle_http part req 0 tot
215sc_w_resp 1
216if eq st 200
217ret 0
218end
219ret 1
220end
221
222fn sc_handle_conn cfd int to void
223req = new uchar 4096 0
224rn = recv cfd sc_sockbuf 0 SRV_MAX_REQ_BYTES int 0
225if gt rn 0
226urn = ulong rn
227for i ulong urn bound 4096
228put req i get sc_sockbuf i
229end
230st = srv_handle_http part req 0 urn
231rlen = srv_resp_len
232soff = ulong 0
233while lt soff rlen bound 4096
234sk = send cfd srv_resp_buf soff sub rlen soff int 0
235if le sk 0
236break
237end
238soff = add soff ulong sk
239end
240end
241cc = fclose cfd
242end
243
244note Serve HTTP/1.1 on loopback 127.0.0.1:port using Linux AArch64 socket host words.
245fn sc_run_serve_tcp port ulong once ulong to int
246opt = new uchar 4 0
247sa = new uchar 16 0
248if not sc_load_env_bearer
249sc_w 2 sc_err_env
250ret 1
251end
252note AF_INET=2, SOCK_STREAM=1, protocol=0
253sfd = socknew int 2 int 1 int 0
254if lt sfd 0
255sc_w 2 sc_err_sock
256ret 1
257end
258note SO_REUSEADDR: level SOL_SOCKET=1, optname SO_REUSEADDR=2, val=1 (4 bytes LE)
259put opt 0 uchar 1
260orc = sockopt int sfd int 1 int 2 opt 0 4
261note sockaddr_in (16 bytes): family=2 LE, port BE, addr=127.0.0.1, zero[8]
262put sa 0 uchar 2
263put sa 1 uchar 0
264put sa 2 uchar and shr port 8 255
265put sa 3 uchar and port 255
266put sa 4 uchar 127
267put sa 5 uchar 0
268put sa 6 uchar 0
269put sa 7 uchar 1
270brc = bind int sfd sa 0 16
271if lt brc 0
272cr = fclose int sfd
273sc_w 2 sc_err_sock
274ret 1
275end
276lrc = listen int sfd int 16
277if lt lrc 0
278cr = fclose int sfd
279sc_w 2 sc_err_sock
280ret 1
281end
282served = ulong 0
283max_reqs = ulong 1000000
284if ne once 0
285max_reqs = ulong 1
286end
287while lt served max_reqs bound 1000000
288cfd = accept int sfd int 0
289if lt cfd 0
290break
291end
292sc_handle_conn int cfd
293served = add served 1
294end
295cs = fclose int sfd
296ret 0
297end
298
299fn sc_run_models to int
300if not sc_load_env_bearer
301sc_w 2 sc_err_env
302ret 1
303end
304req = new uchar 512 0
305w = ulong 0
306for i ulong len sc_req_models_a bound 128
307put req w get sc_req_models_a i
308w = add w 1
309end
310tlen = get srv_auth_meta 1
311for i ulong tlen bound 64
312put req w get srv_auth_tok i
313w = add w 1
314end
315for i ulong len sc_req_models_b bound 8
316put req w get sc_req_models_b i
317w = add w 1
318end
319wire = new uchar 512 0
320wn = sc_expand_crlf part req 0 w wire
321st = srv_handle_http part wire 0 wn
322sc_w_resp 1
323if eq st 200
324ret 0
325end
326ret 1
327end
328
329fn sc_run_chat to int
330krc = kv_init 32 16 1024
331if ne krc 0
332ret 1
333end
334mt = cli_max_tokens
335sc_w 1 sc_chat_out0
336sc_w_u64 1 mt
337sc_w 1 sc_chat_out1
338sc_w_u64 1 kv_available_blocks
339sc_w 1 sc_chat_out2
340ret 0
341end
342
343fn main to int
344wire = new uchar 512 0
345once_box = new ulong 1 0
346line = new uchar 512 0
347ac = argcnt
348if lt ac 2
349sc_w 2 sc_usage
350ret 2
351end
352if sc_arg_eq 1 sc_cmd_stdio
353ret sc_run_serve_stdio
354end
355clen = sc_build_cmdline ulong ac once_box
356for i ulong clen bound 512
357put line i get sc_cmdline i
358end
359drc = cli_dispatch part line 0 clen
360if ne drc CLI_OK
361sc_w 2 sc_err_cli
362sc_w_u64 2 drc
363sc_w 2 sc_nl
364ret 2
365end
366cmd = cli_cmd
367if eq cmd CLI_CMD_VERSION
368sc_w 1 sc_ver
369ret 0
370end
371if eq cmd CLI_CMD_HEALTH
372wn = sc_expand_crlf sc_req_health wire
373st = srv_handle_http part wire 0 wn
374sc_w_resp 1
375ret 0
376end
377if eq cmd CLI_CMD_MODELS
378ret sc_run_models
379end
380if eq cmd CLI_CMD_CHAT
381ret sc_run_chat
382end
383if eq cmd CLI_CMD_SERVE
384ret sc_run_serve_tcp cli_port get once_box 0
385end
386sc_w 2 sc_usage
387ret 2
388end