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
omega-mv / core / cortex / om / api.om
| 1 | note api.om: routes, auth and the Cortex handlers (wire contract: spec/CORTEX-API.md). |
| 2 | note Request strings are staged in hs at hst / hc at hct and committed only by writes. |
| 3 | global hst ulong 1 |
| 4 | global hct ulong 1 |
| 5 | global ap ulong 48 |
| 6 | const A_Q ulong 0 |
| 7 | const A_SPACE ulong 1 |
| 8 | const A_LIMIT ulong 2 |
| 9 | const A_ID ulong 3 |
| 10 | const A_NAME ulong 4 |
| 11 | const A_TYPE ulong 5 |
| 12 | const A_CONT ulong 6 |
| 13 | const A_ALIAS ulong 7 |
| 14 | const A_META ulong 8 |
| 15 | const A_CONF ulong 9 |
| 16 | const A_VF ulong 10 |
| 17 | const A_VT ulong 11 |
| 18 | const A_EMB ulong 12 |
| 19 | const A_KIND ulong 13 |
| 20 | const A_SUBJ ulong 14 |
| 21 | const A_PRED ulong 15 |
| 22 | const A_OBJ ulong 16 |
| 23 | const A_LIT ulong 17 |
| 24 | const A_TT ulong 18 |
| 25 | const A_TID ulong 19 |
| 26 | const A_NOW ulong 20 |
| 27 | const A_T0 ulong 21 |
| 28 | const A_SPSET ulong 22 |
| 29 | const A_ROUTE ulong 23 |
| 30 | const A_ESET ulong 24 |
| 31 | const A_BUDGET ulong 25 |
| 32 | text tdefsp "atlas-memory" |
| 33 | text tdeftype "discovery" |
| 34 | text tempty "" |
| 35 | text pt_wr "/api/cortex/write" |
| 36 | text pt_srch "/api/cortex/search" |
| 37 | text pt_rcl "/api/cortex/recall" |
| 38 | text pt_get "/api/cortex/get" |
| 39 | text pt_trav "/api/cortex/traverse" |
| 40 | text pt_del "/api/cortex/delete" |
| 41 | text pt_hlth "/health" |
| 42 | text pt_stat "/status" |
| 43 | text thealth "{\"runtime\":\"native-arm64-omega\",\"service\":\"cortex-rs\",\"space\":\"atlas-memory\",\"status\":\"ok\",\"version\":\"0.1.0\"}" |
| 44 | text e401 "{\"error\":\"Unauthorized: invalid or missing Cortex bearer token\"}" |
| 45 | text e503 "{\"error\":\"Service unavailable: Cortex authentication token unconfigured on host\"}" |
| 46 | text e403 "{\"error\":\"Forbidden: token scope does not cover this space\"}" |
| 47 | text e400id "{\"error\":\"Missing id or name parameter\"}" |
| 48 | text e404 "{\"error\":\"Entity not found\"}" |
| 49 | text e415 "Expected request with `Content-Type: application/json`" |
| 50 | text e413 "Failed to buffer the request body: length limit exceeded" |
| 51 | text eqs "Failed to deserialize query string: " |
| 52 | text tbearer "Bearer " |
| 53 | text tappl "application" |
| 54 | text tjson "json" |
| 55 | text tjson2 "+json" |
| 56 | text tnull "null" |
| 57 | text ttrue "true" |
| 58 | text tfalse "false" |
| 59 | text tobj0 "{}" |
| 60 | text tarr0 "[]" |
| 61 | |
| 62 | note ---- staging helpers ---- |
| 63 | fn stage_jsv to ulong |
| 64 | n = get jsvl 0 |
| 65 | o = get hst 0 |
| 66 | if gt add o n HSCAP |
| 67 | trap 9 |
| 68 | end |
| 69 | for i ulong n |
| 70 | hsp add o i get jsv i |
| 71 | end |
| 72 | put hst 0 add o n |
| 73 | ret rf_hs o n |
| 74 | end |
| 75 | fn stage_jsv_hc to ulong |
| 76 | n = get jsvl 0 |
| 77 | o = get hct 0 |
| 78 | if gt add o n HCCAP |
| 79 | trap 9 |
| 80 | end |
| 81 | for i ulong n |
| 82 | hc_put add o i get jsv i |
| 83 | end |
| 84 | put hct 0 add o n |
| 85 | ret rf_hc o n |
| 86 | end |
| 87 | fn stage_txt t view uchar * to ulong |
| 88 | o = get hst 0 |
| 89 | for i ulong len t |
| 90 | hsp add o i get t i |
| 91 | end |
| 92 | put hst 0 add o len t |
| 93 | ret rf_hs o len t |
| 94 | end |
| 95 | note stage the JSON output buffer (job[a..jobl)) as an hs string, then drop it from job |
| 96 | fn stage_job a ulong to ulong |
| 97 | n = sub get jobl 0 a |
| 98 | o = get hst 0 |
| 99 | if gt add o n HSCAP |
| 100 | trap 9 |
| 101 | end |
| 102 | for i ulong n |
| 103 | hsp add o i get job add a i |
| 104 | end |
| 105 | put hst 0 add o n |
| 106 | put jobl 0 a |
| 107 | ret rf_hs o n |
| 108 | end |
| 109 | fn st_begin to void |
| 110 | put hst 0 get hsn 0 |
| 111 | put hct 0 get hcn 0 |
| 112 | sp = get ap A_SPSET |
| 113 | r = get ap A_ROUTE |
| 114 | for i ulong len ap |
| 115 | put ap i NONE |
| 116 | end |
| 117 | put ap A_SPSET sp |
| 118 | put ap A_ROUTE r |
| 119 | end |
| 120 | fn st_commit to void |
| 121 | put hsn 0 get hst 0 |
| 122 | put hcn 0 get hct 0 |
| 123 | end |
| 124 | |
| 125 | note ---- JSON output of stored strings ---- |
| 126 | fn o_hs r ulong to void |
| 127 | if eq r NONE |
| 128 | js_raw tnull |
| 129 | ret |
| 130 | end |
| 131 | o = rf_off r |
| 132 | n = rf_len r |
| 133 | for i ulong n |
| 134 | put jes i hsg add o i |
| 135 | end |
| 136 | js_esc_buf 0 n |
| 137 | end |
| 138 | fn o_hc r ulong to void |
| 139 | o = rf_off r |
| 140 | n = rf_len r |
| 141 | for i ulong n |
| 142 | put jes i hc_get add o i |
| 143 | end |
| 144 | js_esc_buf 0 n |
| 145 | end |
| 146 | note canonical JSON of a stored json text; fallback text when it does not parse |
| 147 | fn o_canon r ulong fb view uchar * to void |
| 148 | o = rf_off r |
| 149 | n = rf_len r |
| 150 | for i ulong n |
| 151 | put jes i hsg add o i |
| 152 | end |
| 153 | if not js_canon_buf 0 n |
| 154 | js_raw fb |
| 155 | end |
| 156 | end |
| 157 | text k_id "{\"id\":" |
| 158 | text k_spid ",\"spaceId\":" |
| 159 | text k_spsl ",\"spaceSlug\":" |
| 160 | text k_ety ",\"entityType\":" |
| 161 | text k_cn ",\"canonicalName\":" |
| 162 | text k_ct ",\"content\":" |
| 163 | text k_al ",\"aliases\":" |
| 164 | text k_md ",\"metadata\":" |
| 165 | text k_cf ",\"confidence\":" |
| 166 | text k_rev ",\"revision\":" |
| 167 | text k_ret ",\"retracted\":" |
| 168 | text k_vf ",\"validFrom\":" |
| 169 | text k_vt ",\"validTo\":" |
| 170 | text k_ca ",\"createdAt\":" |
| 171 | note the CortexEntity object without its closing brace |
| 172 | fn o_entity e ulong to void |
| 173 | js_raw k_id |
| 174 | o_hs get eid e |
| 175 | js_raw k_spid |
| 176 | o_hs get esid e |
| 177 | js_raw k_spsl |
| 178 | o_hs get dref get esl e |
| 179 | js_raw k_ety |
| 180 | o_hs get dref get ety e |
| 181 | js_raw k_cn |
| 182 | o_hs get enm e |
| 183 | js_raw k_ct |
| 184 | o_hc get ect e |
| 185 | js_raw k_al |
| 186 | if eq get eav e 1 |
| 187 | o_canon get eal e tarr0 |
| 188 | else |
| 189 | js_raw tarr0 |
| 190 | end |
| 191 | js_raw k_md |
| 192 | o_canon get emd e tobj0 |
| 193 | js_raw k_cf |
| 194 | js_f64 bitcast ulong get ecf e |
| 195 | js_raw k_rev |
| 196 | js_i64 get erev e |
| 197 | js_raw k_ret |
| 198 | if eq get eret e 0 |
| 199 | js_raw tfalse |
| 200 | else |
| 201 | js_raw ttrue |
| 202 | end |
| 203 | js_raw k_vf |
| 204 | o_hs get evf e |
| 205 | js_raw k_vt |
| 206 | o_hs get evt e |
| 207 | js_raw k_ca |
| 208 | o_hs get eca e |
| 209 | end |
| 210 | |
| 211 | note ---- auth ---- |
| 212 | note tok: 0 len of CORTEX_TOKEN from the environment (NONE = unset or blank), 1 HOME len |
| 213 | global tok uchar 4096 |
| 214 | global tokl ulong 2 |
| 215 | global thome uchar 4096 |
| 216 | global tfile uchar 4096 |
| 217 | text ttokpath "/.config/cortex/token" |
| 218 | text tenvtok "CORTEX_TOKEN=" |
| 219 | text tenvhome "HOME=" |
| 220 | global envb uchar 65536 |
| 221 | fn env_get name view uchar * dst ulong to ulong |
| 222 | c = ulong envcnt |
| 223 | for i ulong c |
| 224 | n = envread i envb 0 len envb |
| 225 | if ge n long len name |
| 226 | same = true |
| 227 | for j ulong len name |
| 228 | if ne get envb j get name j |
| 229 | same = false |
| 230 | end |
| 231 | end |
| 232 | if same |
| 233 | rv = env_copy dst len name ulong n |
| 234 | ret rv |
| 235 | end |
| 236 | end |
| 237 | end |
| 238 | ret NONE |
| 239 | end |
| 240 | note dst 0: tok (trimmed), 1: thome |
| 241 | fn env_copy dst ulong a ulong n ulong to ulong |
| 242 | k = sub n a |
| 243 | for i ulong k |
| 244 | put jrs i get envb add a i |
| 245 | end |
| 246 | if eq dst 1 |
| 247 | for i ulong k |
| 248 | put thome i get jrs i |
| 249 | end |
| 250 | ret k |
| 251 | end |
| 252 | ru_trim 0 k |
| 253 | o = get jtro 0 |
| 254 | l = get jtrl 0 |
| 255 | for i ulong l |
| 256 | put tok i get jrs add o i |
| 257 | end |
| 258 | ret l |
| 259 | end |
| 260 | fn auth_init to void |
| 261 | l = env_get tenvtok 0 |
| 262 | if eq l 0 |
| 263 | l = NONE |
| 264 | end |
| 265 | put tokl 0 l |
| 266 | put tokl 1 env_get tenvhome 1 |
| 267 | end |
| 268 | note load the token file when the environment has none (cortex-rs reads it on every request) |
| 269 | fn auth_file to ulong |
| 270 | h = get tokl 1 |
| 271 | if eq h NONE |
| 272 | h = 0 |
| 273 | end |
| 274 | for i ulong h |
| 275 | put tfile i get thome i |
| 276 | end |
| 277 | for i ulong len ttokpath |
| 278 | put tfile add h i get ttokpath i |
| 279 | end |
| 280 | fd = fopen tfile 0 add h len ttokpath 524288 0 |
| 281 | if lt fd 0 |
| 282 | ret 0 |
| 283 | end |
| 284 | n = read int fd jrs 0 4096 |
| 285 | x = fclose int fd |
| 286 | if or lt n 0 ne x 0 |
| 287 | ret 0 |
| 288 | end |
| 289 | ru_trim 0 ulong n |
| 290 | o = get jtro 0 |
| 291 | l = get jtrl 0 |
| 292 | for i ulong l |
| 293 | put tok i get jrs add o i |
| 294 | end |
| 295 | ret l |
| 296 | end |
| 297 | note 0 ok, 401, 503 (and A_SPSET: NONE = full access, else the scoped token's index) |
| 298 | fn auth_check to ulong |
| 299 | l = get tokl 0 |
| 300 | if eq l NONE |
| 301 | l = auth_file |
| 302 | end |
| 303 | if eq l 0 |
| 304 | ret 503 |
| 305 | end |
| 306 | al = get hv H_AL |
| 307 | if eq al NONE |
| 308 | ret 401 |
| 309 | end |
| 310 | ao = get hv H_AO |
| 311 | if lt al len tbearer |
| 312 | ret 401 |
| 313 | end |
| 314 | for i ulong len tbearer |
| 315 | if ne get rq add ao i get tbearer i |
| 316 | ret 401 |
| 317 | end |
| 318 | end |
| 319 | o = add ao len tbearer |
| 320 | n = sub al len tbearer |
| 321 | for i ulong n |
| 322 | put jrs i get rq add o i |
| 323 | end |
| 324 | ru_trim 0 n |
| 325 | if auth_eq get jtro 0 get jtrl 0 l |
| 326 | ret 0 |
| 327 | end |
| 328 | ret scope_check get jtro 0 get jtrl 0 |
| 329 | end |
| 330 | note constant-time compare of jrs[o..o+n) with tok[0..l) |
| 331 | fn auth_eq o ulong n ulong l ulong to bool |
| 332 | d = ulong 0 |
| 333 | if ne n l |
| 334 | d = 1 |
| 335 | end |
| 336 | k = n |
| 337 | if gt k 4096 |
| 338 | k = 4096 |
| 339 | end |
| 340 | for i ulong k |
| 341 | x = ulong get jrs add o i |
| 342 | y = ulong get tok i |
| 343 | d = or d xor x y |
| 344 | end |
| 345 | ret eq d 0 |
| 346 | end |
| 347 | |
| 348 | note ---- routing ---- |
| 349 | note route ids: 1 health/status, 2 write, 3 search, 4 recall, 5 get, 6 traverse, 7 delete (extension), |
| 350 | note 8 .. 15 Company Brain (extension, brain.om bn_route), 0 unknown |
| 351 | fn route_id to ulong |
| 352 | o = get hv H_PO |
| 353 | n = get hv H_PL |
| 354 | if or rq_eq o n pt_hlth rq_eq o n pt_stat |
| 355 | ret 1 |
| 356 | end |
| 357 | if rq_eq o n pt_wr |
| 358 | ret 2 |
| 359 | end |
| 360 | if rq_eq o n pt_srch |
| 361 | ret 3 |
| 362 | end |
| 363 | if rq_eq o n pt_rcl |
| 364 | ret 4 |
| 365 | end |
| 366 | if rq_eq o n pt_get |
| 367 | ret 5 |
| 368 | end |
| 369 | if rq_eq o n pt_trav |
| 370 | ret 6 |
| 371 | end |
| 372 | if rq_eq o n pt_del |
| 373 | ret 7 |
| 374 | end |
| 375 | ret bn_route o n |
| 376 | end |
| 377 | note allow set per route (http_allow codes) |
| 378 | fn route_allow r ulong to ulong |
| 379 | if or eq r 1 eq r 5 |
| 380 | ret 1 |
| 381 | end |
| 382 | if eq r 3 |
| 383 | ret 3 |
| 384 | end |
| 385 | if eq r 0 |
| 386 | ret 0 |
| 387 | end |
| 388 | ret 2 |
| 389 | end |
| 390 | fn route_method_ok r ulong m ulong to bool |
| 391 | if or eq r 1 eq r 5 |
| 392 | ret or eq m 1 eq m 2 |
| 393 | end |
| 394 | if eq r 3 |
| 395 | ret or or eq m 1 eq m 2 eq m 3 |
| 396 | end |
| 397 | ret eq m 3 |
| 398 | end |
| 399 | fn send_txt c int st ulong ct ulong al ulong t view uchar * to void |
| 400 | put jobl 0 0 |
| 401 | js_raw t |
| 402 | http_send c st ct al get jobl 0 |
| 403 | end |
| 404 | |
| 405 | note answer a request that http_read refused (st: 400, 408, 413, 431); no server state is used |
| 406 | fn api_rderr c int st ulong to void |
| 407 | put jobl 0 0 |
| 408 | if eq st 413 |
| 409 | js_raw e413 |
| 410 | http_send c 413 2 0 get jobl 0 |
| 411 | ret |
| 412 | end |
| 413 | http_send c st 0 0 0 |
| 414 | end |
| 415 | note one request on connection c |
| 416 | fn api_conn c int to void |
| 417 | st = http_read c |
| 418 | if eq st 1 |
| 419 | ret |
| 420 | end |
| 421 | if ne st 0 |
| 422 | api_rderr c st |
| 423 | ret |
| 424 | end |
| 425 | r = route_id |
| 426 | put ap A_ROUTE r |
| 427 | m = get hv H_METH |
| 428 | if eq m 4 |
| 429 | http_preflight c route_allow r |
| 430 | ret |
| 431 | end |
| 432 | if eq r 1 |
| 433 | if route_method_ok r m |
| 434 | send_txt c 200 1 0 thealth |
| 435 | else |
| 436 | http_send c 405 0 1 0 |
| 437 | end |
| 438 | ret |
| 439 | end |
| 440 | put ap A_SPSET NONE |
| 441 | au = auth_check |
| 442 | al = ulong 0 |
| 443 | if and ne r 0 not route_method_ok r m |
| 444 | al = route_allow r |
| 445 | end |
| 446 | if eq au 401 |
| 447 | send_txt c 401 1 al e401 |
| 448 | ret |
| 449 | end |
| 450 | if eq au 503 |
| 451 | send_txt c 503 1 al e503 |
| 452 | ret |
| 453 | end |
| 454 | if eq r 0 |
| 455 | http_send c 404 0 0 0 |
| 456 | ret |
| 457 | end |
| 458 | if ne al 0 |
| 459 | http_send c 405 0 al 0 |
| 460 | ret |
| 461 | end |
| 462 | api_dispatch c r m |
| 463 | end |
| 464 | |
| 465 | fn api_dispatch c int r ulong m ulong to void |
| 466 | put jobl 0 0 |
| 467 | st_begin |
| 468 | put ap A_T0 ulong monons |
| 469 | if or eq r 5 and eq r 3 ne m 3 |
| 470 | api_get_or_search c r |
| 471 | ret |
| 472 | end |
| 473 | if not json_ctype |
| 474 | send_txt c 415 2 0 e415 |
| 475 | ret |
| 476 | end |
| 477 | bo = get hv H_BO |
| 478 | bl = get hv H_BL |
| 479 | for i ulong bl |
| 480 | put jin i get rq add bo i |
| 481 | end |
| 482 | put jinl 0 bl |
| 483 | st = ulong 0 |
| 484 | if ge r 8 |
| 485 | st = bn_de r |
| 486 | end |
| 487 | if eq r 2 |
| 488 | st = de_write |
| 489 | end |
| 490 | if eq r 3 |
| 491 | st = de_search |
| 492 | end |
| 493 | if eq r 4 |
| 494 | st = de_recall |
| 495 | end |
| 496 | if eq r 6 |
| 497 | st = de_traverse |
| 498 | end |
| 499 | if eq r 7 |
| 500 | st = de_delete |
| 501 | end |
| 502 | if ne st 0 |
| 503 | http_send c st 2 0 get jobl 0 |
| 504 | ret |
| 505 | end |
| 506 | put jobl 0 0 |
| 507 | st = api_run r |
| 508 | http_send c st 1 0 get jobl 0 |
| 509 | end |
| 510 | fn api_run r ulong to ulong |
| 511 | if ge r 8 |
| 512 | ret bn_run r |
| 513 | end |
| 514 | if eq r 2 |
| 515 | ret api_write |
| 516 | end |
| 517 | if eq r 3 |
| 518 | ret api_search |
| 519 | end |
| 520 | if eq r 4 |
| 521 | ret api_recall |
| 522 | end |
| 523 | if eq r 6 |
| 524 | ret api_traverse |
| 525 | end |
| 526 | ret api_delete |
| 527 | end |
| 528 | fn api_get_or_search c int r ulong to void |
| 529 | st = qs_parse r |
| 530 | if ne st 0 |
| 531 | http_send c st 2 0 get jobl 0 |
| 532 | ret |
| 533 | end |
| 534 | put jobl 0 0 |
| 535 | if eq r 5 |
| 536 | st = api_get |
| 537 | else |
| 538 | st = api_search |
| 539 | end |
| 540 | http_send c st 1 0 get jobl 0 |
| 541 | end |
| 542 | |
| 543 | note axum Json content-type check: mime type application and subtype json or suffix +json |
| 544 | fn json_ctype to bool |
| 545 | n = get hv H_CL |
| 546 | if eq n NONE |
| 547 | ret false |
| 548 | end |
| 549 | o = get hv H_CO |
| 550 | e = add o n |
| 551 | s = o |
| 552 | while and lt s e ne get rq s 59 bound 999999 |
| 553 | s = add s 1 |
| 554 | end |
| 555 | while and gt s o rq_isws get rq sub s 1 bound 999999 |
| 556 | s = sub s 1 |
| 557 | end |
| 558 | sl = o |
| 559 | while and lt sl s ne get rq sl 47 bound 999999 |
| 560 | sl = add sl 1 |
| 561 | end |
| 562 | if ge sl s |
| 563 | ret false |
| 564 | end |
| 565 | if not rq_ieq o sub sl o tappl |
| 566 | ret false |
| 567 | end |
| 568 | st = add sl 1 |
| 569 | sn = sub s st |
| 570 | if rq_ieq st sn tjson |
| 571 | ret true |
| 572 | end |
| 573 | if lt sn len tjson2 |
| 574 | ret false |
| 575 | end |
| 576 | ret rq_ieq sub s len tjson2 len tjson2 tjson2 |
| 577 | end |