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 / core / cortex / om / api.om

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