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