cortex
aien-dev/cortex
Brought by drakestapleton
| 1 | note api2.om: handler bodies (search, recall, get, traverse, writes) on the store. |
| 2 | global rsl ulong 64 |
| 3 | global rsb f64 64 |
| 4 | global rsk uchar 64 |
| 5 | global nres ulong 1 |
| 6 | global qtr uchar 4194304 |
| 7 | global qtrl ulong 1 |
| 8 | global qem f32 768 |
| 9 | global qemf ulong 1 |
| 10 | text r_res "{\"results\":[" |
| 11 | text r_tail "],\"degraded\":[],\"elapsedMs\":" |
| 12 | text r_lex ",\"lexicalScore\":" |
| 13 | text r_gss ",\"graphScore\":0.0,\"semanticScore\":0.0,\"score\":" |
| 14 | text r_ent "{\"entity\":" |
| 15 | text r_fs "},\"finalScore\":" |
| 16 | text r_cs ",\"componentScores\":{\"lexical\":" |
| 17 | text r_sem ",\"semantic\":" |
| 18 | text r_cr ",\"confidenceRecency\":0.95,\"graph\":0.0}}" |
| 19 | text r_claims "{\"claims\":[" |
| 20 | text r_cend "]}" |
| 21 | text e_lkcx "{\"error\":\"LIKE or GLOB pattern too complex\"}" |
| 22 | global lkerr uchar 1 |
| 23 | |
| 24 | note ---- result list ---- |
| 25 | fn res_has e ulong to bool |
| 26 | for i ulong get nres 0 |
| 27 | if eq get rsl i e |
| 28 | ret true |
| 29 | end |
| 30 | end |
| 31 | ret false |
| 32 | end |
| 33 | fn res_add e ulong k uchar b f64 to void |
| 34 | n = get nres 0 |
| 35 | if ge n len rsl |
| 36 | ret |
| 37 | end |
| 38 | put rsl n e |
| 39 | put rsk n k |
| 40 | put rsb n b |
| 41 | put nres 0 add n 1 |
| 42 | end |
| 43 | |
| 44 | note ---- FTS phase: run the MATCH text in qq for space d; fills the result list ---- |
| 45 | note (fts_search: qerr <> 0 means SQLite raised an error; cortex-rs run_fts then yields None = 0 rows) |
| 46 | note qok is set for the FTS rows of space d only (fts_search reads it for no other row) |
| 47 | fn cx_fts_run d ulong lim ulong to void |
| 48 | e = fts_gfirst d |
| 49 | for a ulong 4611686018427387904 |
| 50 | if eq e NONE |
| 51 | break |
| 52 | end |
| 53 | v = uchar 0 |
| 54 | if eq get eret e 0 |
| 55 | v = 1 |
| 56 | end |
| 57 | put qok e v |
| 58 | e = fts_gnext e |
| 59 | end |
| 60 | put qgsel 0 d |
| 61 | put qlim 0 lim |
| 62 | n = fts_search |
| 63 | put nres 0 0 |
| 64 | for i ulong n |
| 65 | res_add get qrd i 0 get qrb i |
| 66 | end |
| 67 | end |
| 68 | note qq = clean_q (qtr without double quotes) inside double quotes |
| 69 | fn qq_clean to void |
| 70 | put qq 0 34 |
| 71 | o = ulong 1 |
| 72 | for i ulong get qtrl 0 |
| 73 | c = get qtr i |
| 74 | if ne c 34 |
| 75 | put qq o c |
| 76 | o = add o 1 |
| 77 | end |
| 78 | end |
| 79 | put qq o 34 |
| 80 | put qql 0 add o 1 |
| 81 | end |
| 82 | |
| 83 | note ---- search ---- |
| 84 | fn api_search to ulong |
| 85 | sp = get ap A_SPACE |
| 86 | if eq sp NONE |
| 87 | sp = stage_txt tdefsp |
| 88 | end |
| 89 | if not scope_ok sp |
| 90 | js_raw e403 |
| 91 | ret 403 |
| 92 | end |
| 93 | lim = get ap A_LIMIT |
| 94 | if eq lim NONE |
| 95 | lim = 12 |
| 96 | end |
| 97 | if gt lim 50 |
| 98 | lim = 50 |
| 99 | end |
| 100 | put nres 0 0 |
| 101 | put lkerr 0 0 |
| 102 | d = dict_find sp |
| 103 | if ne d NONE |
| 104 | search_in d lim |
| 105 | end |
| 106 | if eq get lkerr 0 1 |
| 107 | js_raw e_lkcx |
| 108 | ret 500 |
| 109 | end |
| 110 | js_raw r_res |
| 111 | for i ulong get nres 0 |
| 112 | if gt i 0 |
| 113 | js_byte 44 |
| 114 | end |
| 115 | o_search_row i |
| 116 | end |
| 117 | js_raw r_tail |
| 118 | o_elapsed |
| 119 | js_byte 125 |
| 120 | ret 200 |
| 121 | end |
| 122 | fn search_in d ulong lim ulong to void |
| 123 | q = get ap A_Q |
| 124 | n = ulong 0 |
| 125 | if ne q NONE |
| 126 | n = rf_len q |
| 127 | o = rf_off q |
| 128 | for i ulong n |
| 129 | put jrs i hsg add o i |
| 130 | end |
| 131 | end |
| 132 | ru_trim 0 n |
| 133 | tro = get jtro 0 |
| 134 | tl = get jtrl 0 |
| 135 | for i ulong tl |
| 136 | put qtr i get jrs add tro i |
| 137 | end |
| 138 | put qtrl 0 tl |
| 139 | if eq tl 0 |
| 140 | search_list d lim |
| 141 | ret |
| 142 | end |
| 143 | note the live binary returns rows only from the quoted-phrase FTS attempt: its operator query and |
| 144 | note its AND-joined query never contribute (spec/ORACLE-OBSERVED.md) |
| 145 | qq_clean |
| 146 | cx_fts_run d lim |
| 147 | if lt get nres 0 lim |
| 148 | search_like d lim |
| 149 | end |
| 150 | if gt get nres 0 lim |
| 151 | put nres 0 lim |
| 152 | end |
| 153 | end |
| 154 | note substring fallback: LIKE '%q%' on name or content, scan order, LIMIT lim rows, new ones appended |
| 155 | note likeFunc raises "LIKE or GLOB pattern too complex" on the first row it tests (lkerr = 1) |
| 156 | fn search_like d ulong lim ulong to void |
| 157 | n = get qtrl 0 |
| 158 | put qlp 0 37 |
| 159 | for i ulong n |
| 160 | put qlp add i 1 get qtr i |
| 161 | end |
| 162 | put qlp add n 1 37 |
| 163 | put qlpl 0 add n 2 |
| 164 | lok = like_set |
| 165 | lit = like_lit |
| 166 | eo_range d |
| 167 | a = get eor 0 |
| 168 | b = get eor 1 |
| 169 | got = ulong 0 |
| 170 | k = a |
| 171 | while and lt k b lt got lim bound 999999 |
| 172 | e = get eord k |
| 173 | if eq get eret e 0 |
| 174 | if not lok |
| 175 | put lkerr 0 1 |
| 176 | ret |
| 177 | end |
| 178 | if like_ent e lit |
| 179 | got = add got 1 |
| 180 | if not res_has e |
| 181 | res_add e 1 0.0 |
| 182 | end |
| 183 | end |
| 184 | end |
| 185 | k = add k 1 |
| 186 | end |
| 187 | end |
| 188 | note e's name or content LIKE the pattern (lit: like_lit held, like_find answers) |
| 189 | fn like_ent e ulong lit bool to bool |
| 190 | cx_lkname get enm e |
| 191 | if cx_lkone lit |
| 192 | ret true |
| 193 | end |
| 194 | cx_lkcont get ect e |
| 195 | ret cx_lkone lit |
| 196 | end |
| 197 | note the name string r into qct/qctl |
| 198 | fn cx_lkname r ulong to void |
| 199 | o = rf_off r |
| 200 | n = rf_len r |
| 201 | i = ulong 0 |
| 202 | for a ulong 4611686018427387904 |
| 203 | if ge i n |
| 204 | break |
| 205 | end |
| 206 | put qct i hsg add o i |
| 207 | i = add i 1 |
| 208 | end |
| 209 | put qctl 0 n |
| 210 | end |
| 211 | note the content string c into qct/qctl (hc packs eight bytes per word, low byte first) |
| 212 | fn cx_lkcont c ulong to void |
| 213 | o = rf_off c |
| 214 | n = rf_len c |
| 215 | k = ulong 0 |
| 216 | for a ulong 4611686018427387904 |
| 217 | if ge k n |
| 218 | break |
| 219 | end |
| 220 | w = add o k |
| 221 | x = get hc shr w 3 |
| 222 | s = mul and w 7 8 |
| 223 | put qct k uchar and shr x s 255 |
| 224 | k = add k 1 |
| 225 | end |
| 226 | put qctl 0 n |
| 227 | end |
| 228 | fn cx_lkone lit bool to bool |
| 229 | if lit |
| 230 | ret like_find |
| 231 | end |
| 232 | ret like_test |
| 233 | end |
| 234 | note empty query: newest first by created_at (stable on scan order), LIMIT lim |
| 235 | fn search_list d ulong lim ulong to void |
| 236 | eo_range d |
| 237 | a = get eor 0 |
| 238 | b = get eor 1 |
| 239 | for k ulong sub b a |
| 240 | e = get eord add a k |
| 241 | if eq get eret e 0 |
| 242 | list_insert e lim |
| 243 | end |
| 244 | end |
| 245 | end |
| 246 | fn list_insert e ulong lim ulong to void |
| 247 | n = get nres 0 |
| 248 | p = n |
| 249 | while gt p 0 bound 64 |
| 250 | if ge hs_cmp get eca get rsl sub p 1 get eca e 0 |
| 251 | break |
| 252 | end |
| 253 | p = sub p 1 |
| 254 | end |
| 255 | if ge p lim |
| 256 | ret |
| 257 | end |
| 258 | m = n |
| 259 | if ge m lim |
| 260 | m = sub lim 1 |
| 261 | end |
| 262 | i = m |
| 263 | while gt i p bound 64 |
| 264 | put rsl i get rsl sub i 1 |
| 265 | put rsk i 2 |
| 266 | i = sub i 1 |
| 267 | end |
| 268 | put rsl p e |
| 269 | put rsk p 2 |
| 270 | if lt n lim |
| 271 | put nres 0 add n 1 |
| 272 | end |
| 273 | end |
| 274 | fn o_search_row i ulong to void |
| 275 | e = get rsl i |
| 276 | k = get rsk i |
| 277 | s = f64 0.4 |
| 278 | if eq k 0 |
| 279 | s = div 1.0 add 1.0 fabs get rsb i |
| 280 | end |
| 281 | if eq k 1 |
| 282 | s = 0.7 |
| 283 | end |
| 284 | o_entity e |
| 285 | js_raw r_lex |
| 286 | js_f64 bitcast ulong s |
| 287 | js_raw r_gss |
| 288 | js_f64 bitcast ulong s |
| 289 | js_byte 125 |
| 290 | end |
| 291 | fn o_elapsed to void |
| 292 | t = sub ulong monons get ap A_T0 |
| 293 | ms = mul div f64 t 1000000000.0 1000.0 |
| 294 | js_f64 bitcast ulong ms |
| 295 | end |
| 296 | |
| 297 | note ---- recall ---- |
| 298 | global rcf f64 64 |
| 299 | global rcl f64 64 |
| 300 | global rcs f64 64 |
| 301 | fn api_recall to ulong |
| 302 | sp = get ap A_SPACE |
| 303 | if eq sp NONE |
| 304 | sp = stage_txt tdefsp |
| 305 | end |
| 306 | if not scope_ok sp |
| 307 | js_raw e403 |
| 308 | ret 403 |
| 309 | end |
| 310 | lim = get ap A_LIMIT |
| 311 | if eq lim NONE |
| 312 | lim = 5 |
| 313 | end |
| 314 | if gt lim 20 |
| 315 | lim = 20 |
| 316 | end |
| 317 | enc_fetch_q |
| 318 | put nres 0 0 |
| 319 | d = dict_find sp |
| 320 | if ne d NONE |
| 321 | recall_in d lim |
| 322 | end |
| 323 | js_raw r_res |
| 324 | for i ulong get nres 0 |
| 325 | if gt i 0 |
| 326 | js_byte 44 |
| 327 | end |
| 328 | js_raw r_ent |
| 329 | o_entity get rsl i |
| 330 | js_raw r_fs |
| 331 | js_f64 bitcast ulong get rcf i |
| 332 | js_raw r_cs |
| 333 | js_f64 bitcast ulong get rcl i |
| 334 | js_raw r_sem |
| 335 | js_f64 bitcast ulong get rcs i |
| 336 | js_raw r_cr |
| 337 | end |
| 338 | js_raw r_tail |
| 339 | o_elapsed |
| 340 | js_byte 125 |
| 341 | ret 200 |
| 342 | end |
| 343 | fn recall_in d ulong lim ulong to void |
| 344 | q = get ap A_Q |
| 345 | o = rf_off q |
| 346 | n = rf_len q |
| 347 | for i ulong n |
| 348 | put jrq i hsg add o i |
| 349 | end |
| 350 | put jrql 0 n |
| 351 | ru_prep |
| 352 | eo_range d |
| 353 | a = get eor 0 |
| 354 | b = get eor 1 |
| 355 | for k ulong sub b a |
| 356 | e = get eord add a k |
| 357 | if eq get eret e 0 |
| 358 | recall_one e lim |
| 359 | end |
| 360 | end |
| 361 | end |
| 362 | fn recall_one e ulong lim ulong to void |
| 363 | sem = f64 0.0 |
| 364 | if and eq get qemf 0 1 ne get eem e NONE |
| 365 | sem = cos_em e |
| 366 | end |
| 367 | lex = recall_lex e |
| 368 | fin = lex |
| 369 | if eq get qemf 0 1 |
| 370 | fin = add mul 0.6 sem mul 0.4 lex |
| 371 | end |
| 372 | recall_insert e fin lex sem lim |
| 373 | end |
| 374 | fn recall_lex e ulong to f64 |
| 375 | r = get enm e |
| 376 | o = rf_off r |
| 377 | n = rf_len r |
| 378 | for i ulong n |
| 379 | put jrs i hsg add o i |
| 380 | end |
| 381 | put jrsl 0 n |
| 382 | if ru_contains |
| 383 | ret 0.9 |
| 384 | end |
| 385 | c = get ect e |
| 386 | o = rf_off c |
| 387 | n = rf_len c |
| 388 | for i ulong n |
| 389 | put jrs i hc_get add o i |
| 390 | end |
| 391 | put jrsl 0 n |
| 392 | if ru_contains |
| 393 | ret 0.6 |
| 394 | end |
| 395 | ret 0.0 |
| 396 | end |
| 397 | note stable top-k: a new item goes after every kept item with a score >= its own |
| 398 | fn recall_insert e ulong fin f64 lex f64 sem f64 lim ulong to void |
| 399 | n = get nres 0 |
| 400 | p = n |
| 401 | while gt p 0 bound 64 |
| 402 | if ge get rcf sub p 1 fin |
| 403 | break |
| 404 | end |
| 405 | p = sub p 1 |
| 406 | end |
| 407 | if ge p lim |
| 408 | ret |
| 409 | end |
| 410 | m = n |
| 411 | if ge m lim |
| 412 | m = sub lim 1 |
| 413 | end |
| 414 | i = m |
| 415 | while gt i p bound 64 |
| 416 | j = sub i 1 |
| 417 | put rsl i get rsl j |
| 418 | put rcf i get rcf j |
| 419 | put rcl i get rcl j |
| 420 | put rcs i get rcs j |
| 421 | i = j |
| 422 | end |
| 423 | put rsl p e |
| 424 | put rcf p fin |
| 425 | put rcl p lex |
| 426 | put rcs p sem |
| 427 | if lt n lim |
| 428 | put nres 0 add n 1 |
| 429 | end |
| 430 | end |
| 431 | note cosine similarity in f32 exactly as embeddings.rs (sequential sums, sqrt each, one divide) |
| 432 | fn cos_em e ulong to f64 |
| 433 | r = get eem e |
| 434 | if ne rf_len r 3072 |
| 435 | ret 0.0 |
| 436 | end |
| 437 | o = rf_off r |
| 438 | dot = f32 0.0 |
| 439 | na = f32 0.0 |
| 440 | nb = f32 0.0 |
| 441 | for i ulong 768 |
| 442 | a = get qem i |
| 443 | b = em_at add o mul i 4 |
| 444 | dot = add dot mul a b |
| 445 | na = add na mul a a |
| 446 | nb = add nb mul b b |
| 447 | end |
| 448 | den = mul fsqrt na fsqrt nb |
| 449 | if gt den 0.0 |
| 450 | ret f64 div dot den |
| 451 | end |
| 452 | ret 0.0 |
| 453 | end |
| 454 | note little-endian f32 at hc byte offset o |
| 455 | fn em_at o ulong to f32 |
| 456 | v = or or ulong hc_get o shl ulong hc_get add o 1 8 or shl ulong hc_get add o 2 16 shl ulong hc_get add o 3 24 |
| 457 | ret bitcast f32 uint v |
| 458 | end |
| 459 | |
| 460 | note ---- get ---- |
| 461 | fn api_get to ulong |
| 462 | t = get ap A_ID |
| 463 | if eq t NONE |
| 464 | t = get ap A_NAME |
| 465 | end |
| 466 | if or eq t NONE eq rf_len t 0 |
| 467 | js_raw e400id |
| 468 | ret 400 |
| 469 | end |
| 470 | sp = get ap A_SPACE |
| 471 | if eq sp NONE |
| 472 | sp = stage_txt tdefsp |
| 473 | end |
| 474 | if not scope_ok sp |
| 475 | js_raw e403 |
| 476 | ret 403 |
| 477 | end |
| 478 | d = dict_find sp |
| 479 | if eq d NONE |
| 480 | js_raw e404 |
| 481 | ret 404 |
| 482 | end |
| 483 | eo_range d |
| 484 | a = get eor 0 |
| 485 | b = get eor 1 |
| 486 | for k ulong sub b a |
| 487 | e = get eord add a k |
| 488 | if eq get eret e 0 |
| 489 | if or hs_eq get eid e t hs_eq get enm e t |
| 490 | o_entity e |
| 491 | js_byte 125 |
| 492 | ret 200 |
| 493 | end |
| 494 | end |
| 495 | end |
| 496 | js_raw e404 |
| 497 | ret 404 |
| 498 | end |
| 499 | |
| 500 | note ---- traverse ---- |
| 501 | text k_csub ",\"subjectEntityId\":" |
| 502 | text k_cprd ",\"predicate\":" |
| 503 | text k_cobj ",\"objectEntityId\":" |
| 504 | text k_clit ",\"literalValue\":" |
| 505 | text k_cf0 "{\"confidence\":" |
| 506 | text k_cid ",\"id\":" |
| 507 | fn api_traverse to ulong |
| 508 | s = get ap A_SUBJ |
| 509 | sp = get ap A_SPACE |
| 510 | if eq sp NONE |
| 511 | sp = stage_txt tdefsp |
| 512 | end |
| 513 | if not scope_ok sp |
| 514 | js_raw e403 |
| 515 | ret 403 |
| 516 | end |
| 517 | d = dict_find sp |
| 518 | js_raw r_claims |
| 519 | k = ulong 0 |
| 520 | if ne d NONE |
| 521 | for c ulong get nc 0 |
| 522 | if cl_in c d |
| 523 | if hs_eq get csub c s |
| 524 | k = o_claim_sep c k |
| 525 | end |
| 526 | end |
| 527 | end |
| 528 | for c ulong get nc 0 |
| 529 | if cl_in c d |
| 530 | if and hs_eq get cobj c s not hs_eq get csub c s |
| 531 | k = o_claim_sep c k |
| 532 | end |
| 533 | end |
| 534 | end |
| 535 | end |
| 536 | js_raw r_cend |
| 537 | ret 200 |
| 538 | end |
| 539 | fn cl_in c ulong d ulong to bool |
| 540 | ret and eq get cret c 0 eq get csl c d |
| 541 | end |
| 542 | fn o_claim_sep c ulong k ulong to ulong |
| 543 | if gt k 0 |
| 544 | js_byte 44 |
| 545 | end |
| 546 | o_claim c |
| 547 | ret add k 1 |
| 548 | end |
| 549 | note json!({"claims": claims}) turns each Claim into a Value: keys come out sorted (BTreeMap) |
| 550 | fn o_claim c ulong to void |
| 551 | js_raw k_cf0 |
| 552 | js_f64 bitcast ulong get ccf c |
| 553 | js_raw k_ca |
| 554 | o_hs get cca c |
| 555 | js_raw k_cid |
| 556 | o_hs get cid c |
| 557 | js_raw k_clit |
| 558 | l = get clit c |
| 559 | if eq l NONE |
| 560 | js_raw tnull |
| 561 | else |
| 562 | o_canon l tnull |
| 563 | end |
| 564 | js_raw k_md |
| 565 | o_canon get cmd c tobj0 |
| 566 | js_raw k_cobj |
| 567 | o_hs get cobj c |
| 568 | js_raw k_cprd |
| 569 | o_hs get cprd c |
| 570 | js_raw k_ret |
| 571 | if eq get cret c 0 |
| 572 | js_raw tfalse |
| 573 | else |
| 574 | js_raw ttrue |
| 575 | end |
| 576 | js_raw k_spid |
| 577 | o_hs get csid c |
| 578 | js_raw k_spsl |
| 579 | o_hs get dref get csl c |
| 580 | js_raw k_csub |
| 581 | o_hs get csub c |
| 582 | js_byte 125 |
| 583 | end |