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 / seed / helix / ollm_factory / om_retriever.om

767 lines · 13910 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/helix/ollm_factory/om_retriever.om:
4note Zero-Weight In-Context Verified Idiom Retriever (Pure Seeded OM-2).
5note Indexes NUL-delimited verified zero-leakage OM-2 corpora by structural
6note signature shape (1..5), algorithmic primitive tags, word overlap, and
7note character trigrams. Prepends top-K structurally closest verified idioms
8note within a byte budget before the query prompt in sub-millisecond time.
9note
10note Usage:
11note om_retriever retrieve CORPUS_OM PROMPT_FILE TOP_K MAX_BYTES OUT_PROMPT
12note om_retriever selfcheck
13
14global rCorp uchar *
15global rPrm uchar *
16global rOut uchar 16384
17global rPath uchar 4096
18global rQTok ulong 64
19global rQTri ulong 256
20global rTopOff ulong 8
21global rTopLen ulong 8
22global rTopSc ulong 8
23global rSt ulong 16
24
25text tModeRet "retrieve"
26text tModeSelf "selfcheck"
27text tUsage "usage: om_retriever retrieve CORPUS PROMPT TOP_K MAX_BYTES OUT | selfcheck\n"
28text tErrIo "om_retriever: I/O error\n"
29text tRetA "RETRIEVE top_k="
30text tRetB " corpus_records="
31text tRetC " kept="
32text tRetD " best_score="
33text tRetE " bytes="
34text tSelfOk "OM_RETRIEVER SELFCHECK PASS records=4 top1_matched=1 exact_excluded=1 budget_ok=1\n"
35text tSelfFail "OM_RETRIEVER SELFCHECK FAIL\n"
36text tNl "\n"
37
38text tSelfCorp "note return sum of squares of a and b\nfn s1 a ulong b ulong to ulong\nret add mul a a mul b b\nend\n|note return bit population count of a\nfn s2 a ulong to ulong\nc = ulong 0\nx = a\nwhile gt x 0 bound 64\nc = add c and x 1\nx = shr x 1\nend\nret c\nend\n|note count set bits in unsigned integer a\nfn s3 a ulong to ulong\nc = ulong 0\nx = a\nwhile gt x 0 bound 64\nc = add c and x 1\nx = shr x 1\nend\nret c\nend\n|note reverse array a of length n in place\nfn s4 a edit ulong 8 n ulong to void\ni = ulong 0\nwhile lt mul i 2 n bound 8\nj = sub sub n 1 i\nt = get a i\nput a i get a j\nput a j t\ni = add i 1\nend\nend\n|"
39text tSelfQry "note count set bits in unsigned integer a\nfn solve a ulong to ulong\n"
40
41text kwView "view"
42text kwEdit "edit"
43text kwVoid "void"
44text kwB " b ulong"
45text kwC " c ulong"
46text tgBit "bit"
47text tgMask "mask"
48text tgXor "xor"
49text tgShift "shift"
50text tgPop "pop"
51text tgDig "digit"
52text tgDec "decimal"
53text tgBase "base"
54text tgSum "sum"
55text tgProd "prod"
56text tgCnt "count"
57text tgMax "max"
58text tgMin "min"
59text tgClamp "clamp"
60text tgAbs "abs"
61text tgDiff "diff"
62text tgRev "reverse"
63text tgSort "sort"
64text tgSwap "swap"
65text tgArr "array"
66text tgGcd "gcd"
67text tgPrime "prime"
68text tgDiv "div"
69text tgMod "mod"
70text tgRem "rem"
71text tgFib "fib"
72text tgSqrt "sqrt"
73text tgPow "pow"
74text tgEven "even"
75text tgOdd "odd"
76
77fn rWrt fd int s view uchar * to void
78r = write fd s 0 len s
79end
80
81fn rNum fd int v ulong to void
82b = new uchar 24 0
83p = ulong 24
84x = v
85while true bound 24
86p = sub p 1
87put b p uchar add 48 rem x 10
88x = div x 10
89if eq x 0
90break
91end
92end
93r = write fd b p sub 24 p
94end
95
96fn rEqTxt a view uchar * al ulong b view uchar * to bool
97if ne al len b
98ret false
99end
100for i ulong al bound 64
101if ne get a i get b i
102ret false
103end
104end
105ret true
106end
107
108fn rParseU s view uchar * n ulong to ulong
109v = ulong 0
110for i ulong n bound 32
111c = get s i
112if and ge c 48 le c 57
113v = add mul v 10 ulong sub c 48
114end
115end
116ret v
117end
118
119fn rLower c ulong to ulong
120if and ge c 65 le c 90
121ret add c 32
122end
123ret c
124end
125
126fn rIsAlnum c ulong to bool
127if and ge c 48 le c 57
128ret true
129end
130if and ge c 65 le c 90
131ret true
132end
133if and ge c 97 le c 122
134ret true
135end
136ret false
137end
138
139note Direct substring checks against rCorp and rPrm (avoid passing globals as view *)
140fn rHasSubCorp boff ulong blen ulong sub_s view uchar * to bool
141sl = len sub_s
142if or eq sl 0 gt sl blen
143ret false
144end
145lim = add sub blen sl 1
146for i ulong lim bound 4096
147ok = true
148for j ulong sl bound 64
149if ne rLower ulong get rCorp add boff add i j ulong get sub_s j
150ok = false
151break
152end
153end
154if ok
155ret true
156end
157end
158ret false
159end
160
161fn rHasSubPrm boff ulong blen ulong sub_s view uchar * to bool
162sl = len sub_s
163if or eq sl 0 gt sl blen
164ret false
165end
166lim = add sub blen sl 1
167for i ulong lim bound 4096
168ok = true
169for j ulong sl bound 64
170if ne rLower ulong get rPrm add boff add i j ulong get sub_s j
171ok = false
172break
173end
174end
175if ok
176ret true
177end
178end
179ret false
180end
181
182fn rShapeCorp off ulong n ulong to ulong
183if rHasSubCorp off n kwEdit
184ret 5
185end
186if rHasSubCorp off n kwView
187ret 4
188end
189if rHasSubCorp off n kwC
190ret 3
191end
192if rHasSubCorp off n kwB
193ret 2
194end
195if rHasSubCorp off n kwVoid
196ret 5
197end
198ret 1
199end
200
201fn rShapePrm off ulong n ulong to ulong
202if rHasSubPrm off n kwEdit
203ret 5
204end
205if rHasSubPrm off n kwView
206ret 4
207end
208if rHasSubPrm off n kwC
209ret 3
210end
211if rHasSubPrm off n kwB
212ret 2
213end
214if rHasSubPrm off n kwVoid
215ret 5
216end
217ret 1
218end
219
220fn rTagsCorp off ulong n ulong to ulong
221m = ulong 0
222if or rHasSubCorp off n tgBit or rHasSubCorp off n tgMask or rHasSubCorp off n tgXor or rHasSubCorp off n tgShift rHasSubCorp off n tgPop
223m = or m 1
224end
225if or rHasSubCorp off n tgDig or rHasSubCorp off n tgDec rHasSubCorp off n tgBase
226m = or m 2
227end
228if or rHasSubCorp off n tgSum or rHasSubCorp off n tgProd rHasSubCorp off n tgCnt
229m = or m 4
230end
231if or rHasSubCorp off n tgMax or rHasSubCorp off n tgMin or rHasSubCorp off n tgClamp or rHasSubCorp off n tgAbs rHasSubCorp off n tgDiff
232m = or m 8
233end
234if or rHasSubCorp off n tgRev or rHasSubCorp off n tgSort or rHasSubCorp off n tgSwap rHasSubCorp off n tgArr
235m = or m 16
236end
237if or rHasSubCorp off n tgGcd or rHasSubCorp off n tgPrime or rHasSubCorp off n tgDiv or rHasSubCorp off n tgMod rHasSubCorp off n tgRem
238m = or m 32
239end
240if or rHasSubCorp off n tgFib or rHasSubCorp off n tgSqrt rHasSubCorp off n tgPow
241m = or m 64
242end
243if or rHasSubCorp off n tgEven rHasSubCorp off n tgOdd
244m = or m 128
245end
246ret m
247end
248
249fn rTagsPrm off ulong n ulong to ulong
250m = ulong 0
251if or rHasSubPrm off n tgBit or rHasSubPrm off n tgMask or rHasSubPrm off n tgXor or rHasSubPrm off n tgShift rHasSubPrm off n tgPop
252m = or m 1
253end
254if or rHasSubPrm off n tgDig or rHasSubPrm off n tgDec rHasSubPrm off n tgBase
255m = or m 2
256end
257if or rHasSubPrm off n tgSum or rHasSubPrm off n tgProd rHasSubPrm off n tgCnt
258m = or m 4
259end
260if or rHasSubPrm off n tgMax or rHasSubPrm off n tgMin or rHasSubPrm off n tgClamp or rHasSubPrm off n tgAbs rHasSubPrm off n tgDiff
261m = or m 8
262end
263if or rHasSubPrm off n tgRev or rHasSubPrm off n tgSort or rHasSubPrm off n tgSwap rHasSubPrm off n tgArr
264m = or m 16
265end
266if or rHasSubPrm off n tgGcd or rHasSubPrm off n tgPrime or rHasSubPrm off n tgDiv or rHasSubPrm off n tgMod rHasSubPrm off n tgRem
267m = or m 32
268end
269if or rHasSubPrm off n tgFib or rHasSubPrm off n tgSqrt rHasSubPrm off n tgPow
270m = or m 64
271end
272if or rHasSubPrm off n tgEven rHasSubPrm off n tgOdd
273m = or m 128
274end
275ret m
276end
277
278fn rIndexTrigrams l1e ulong to void
279trc = ulong 0
280if ge l1e 3
281lim = sub l1e 2
282for j ulong lim bound 4096
283if ge trc 256
284break
285end
286c0 = rLower ulong get rPrm j
287c1 = rLower ulong get rPrm add j 1
288c2 = rLower ulong get rPrm add j 2
289g = or shl c0 16 or shl c1 8 c2
290put rQTri trc g
291trc = add trc 1
292end
293end
294put rSt 5 trc
295end
296
297fn rIndexQuery qlen ulong to void
298l1e = ulong 0
299while lt l1e qlen bound 16384
300if eq get rPrm l1e 10
301break
302end
303l1e = add l1e 1
304end
305l2s = l1e
306if lt l2s qlen
307l2s = add l2s 1
308end
309l2e = l2s
310while lt l2e qlen bound 16384
311if eq get rPrm l2e 10
312break
313end
314l2e = add l2e 1
315end
316put rSt 0 0
317put rSt 1 l1e
318put rSt 2 rShapePrm l2s sub l2e l2s
319put rSt 3 rTagsPrm 0 l1e
320
321tc = ulong 0
322i = ulong 0
323while lt i l1e bound 4096
324while lt i l1e bound 4096
325if rIsAlnum ulong get rPrm i
326break
327end
328i = add i 1
329end
330ws = i
331h = ulong 2166136261
332while lt i l1e bound 4096
333c = ulong get rPrm i
334if not rIsAlnum c
335break
336end
337h = mul xor h rLower c 16777619
338i = add i 1
339end
340wl = sub i ws
341if and ge wl 2 lt tc 64
342dup = false
343for k ulong tc bound 64
344if eq get rQTok k h
345dup = true
346break
347end
348end
349if not dup
350put rQTok tc h
351tc = add tc 1
352end
353end
354end
355put rSt 4 tc
356rIndexTrigrams l1e
357end
358
359fn rSameNote coff ulong clen ulong to bool
360qlen = get rSt 1
361if ne clen qlen
362ret false
363end
364for i ulong clen bound 4096
365if ne rLower ulong get rCorp add coff i rLower ulong get rPrm i
366ret false
367end
368end
369ret true
370end
371
372fn rScoreWords off ulong l1e ulong to ulong
373sc = ulong 0
374tc = get rSt 4
375i = ulong 0
376while lt i l1e bound 4096
377while lt i l1e bound 4096
378if rIsAlnum ulong get rCorp add off i
379break
380end
381i = add i 1
382end
383ws = i
384h = ulong 2166136261
385while lt i l1e bound 4096
386c = ulong get rCorp add off i
387if not rIsAlnum c
388break
389end
390h = mul xor h rLower c 16777619
391i = add i 1
392end
393wl = sub i ws
394if ge wl 2
395for k ulong tc bound 64
396if eq get rQTok k h
397sc = add sc 120
398break
399end
400end
401end
402end
403ret sc
404end
405
406fn rScoreTrigrams off ulong l1e ulong to ulong
407trc = get rSt 5
408if or lt l1e 3 eq trc 0
409ret 0
410end
411lim = sub l1e 2
412if gt lim 128
413lim = 128
414end
415hits = ulong 0
416for j ulong lim bound 128
417c0 = rLower ulong get rCorp add off j
418c1 = rLower ulong get rCorp add off add j 1
419c2 = rLower ulong get rCorp add off add j 2
420g = or shl c0 16 or shl c1 8 c2
421for k ulong trc bound 256
422if eq get rQTri k g
423hits = add hits 1
424break
425end
426end
427end
428ret mul hits 15
429end
430
431fn rTagBonus qt ulong rt ulong to ulong
432both = and qt rt
433sc = ulong 0
434for b ulong 8 bound 8
435if ne and shr both b 1 0
436sc = add sc 80
437end
438end
439ret sc
440end
441
442fn rScoreRec off ulong rlen ulong to ulong
443if lt rlen 16
444ret 0
445end
446l1e = ulong 0
447while lt l1e rlen bound 4096
448if eq get rCorp add off l1e 10
449break
450end
451l1e = add l1e 1
452end
453if lt l1e 6
454ret 0
455end
456if rSameNote off l1e
457ret 0
458end
459l2s = l1e
460if lt l2s rlen
461l2s = add l2s 1
462end
463l2e = l2s
464while lt l2e rlen bound 4096
465if eq get rCorp add off l2e 10
466break
467end
468l2e = add l2e 1
469end
470sc = ulong 1
471if eq rShapeCorp add off l2s sub l2e l2s get rSt 2
472sc = add sc 400
473end
474sc = add sc rTagBonus get rSt 3 rTagsCorp off l1e
475sc = add sc rScoreWords off l1e
476sc = add sc rScoreTrigrams off l1e
477if le rlen 280
478sc = add sc 30
479end
480ret sc
481end
482
483fn rInsertTop off ulong rlen ulong sc ulong topk ulong to void
484if or eq sc 0 eq topk 0
485ret
486end
487idx = topk
488for i ulong topk bound 8
489if gt sc get rTopSc i
490idx = i
491break
492end
493end
494if ge idx topk
495ret
496end
497j = sub topk 1
498while gt j idx bound 8
499put rTopOff j get rTopOff sub j 1
500put rTopLen j get rTopLen sub j 1
501put rTopSc j get rTopSc sub j 1
502j = sub j 1
503end
504put rTopOff idx off
505put rTopLen idx rlen
506put rTopSc idx sc
507end
508
509fn rAppendCorp so ulong sl ulong opos ulong to ulong
510p = opos
511for j ulong sl bound 4096
512if lt p 16384
513put rOut p get rCorp add so j
514p = add p 1
515end
516end
517ret p
518end
519
520fn rAppendPrm qlen ulong opos ulong to ulong
521p = opos
522for j ulong qlen bound 16384
523if lt p 16384
524put rOut p get rPrm j
525p = add p 1
526end
527end
528ret p
529end
530
531fn rAssembleOut qlen ulong to ulong
532put rSt 12 0
533put rSt 9 0
534for i ulong get rSt 10 bound 8
535sl = get rTopLen i
536if and gt sl 0 gt get rTopSc i 0
537if le add get rSt 12 add sl 1 get rSt 11
538p = rAppendCorp get rTopOff i sl get rSt 12
539if and gt p 0 ne get rOut sub p 1 10
540if lt p 16384
541put rOut p 10
542p = add p 1
543end
544end
545put rSt 12 p
546put rSt 9 add get rSt 9 1
547end
548end
549end
550p2 = rAppendPrm qlen get rSt 12
551put rSt 12 p2
552ret p2
553end
554
555fn rRunRetrieve clen ulong qlen ulong to ulong
556topk = get rSt 10
557rIndexQuery qlen
558for i ulong 8 bound 8
559put rTopOff i 0
560put rTopLen i 0
561put rTopSc i 0
562end
563recs = ulong 0
564pos = ulong 0
565while lt pos clen bound 100000000
566s = pos
567while lt pos clen bound 100000000
568if eq get rCorp pos 0
569break
570end
571pos = add pos 1
572end
573rlen = sub pos s
574if gt rlen 0
575recs = add recs 1
576sc = rScoreRec s rlen
577rInsertTop s rlen sc topk
578end
579if lt pos clen
580pos = add pos 1
581end
582end
583put rSt 8 recs
584ret rAssembleOut qlen
585end
586
587fn rLoadPath argi ulong to ulong
588n = argread argi rPath 0 4095
589if le n 0
590ret 0
591end
592ret ulong n
593end
594
595fn rSelfcheck to int
596sl = len tSelfCorp
597er = enlarge rCorp add sl 1
598ql = len tSelfQry
599er2 = enlarge rPrm add ql 1
600for i ulong sl bound 1024
601c = get tSelfCorp i
602if eq c 124
603put rCorp i 0
604else
605put rCorp i c
606end
607end
608for i ulong ql bound 1024
609put rPrm i get tSelfQry i
610end
611put rSt 10 2
612put rSt 11 500
613olen = rRunRetrieve sl ql
614recs = get rSt 8
615kept = get rSt 9
616best = get rTopSc 0
617if or ne recs 4 or lt kept 1 or lt best 500 eq olen 0
618rWrt 2 tSelfFail
619ret 1
620end
621t0 = get rTopOff 0
622if or lt t0 50 gt t0 150
623rWrt 2 tSelfFail
624ret 1
625end
626rWrt 1 tSelfOk
627ret 0
628end
629
630fn rLoadCorp argi ulong to ulong
631pl = rLoadPath argi
632fd = fopen rPath 0 pl 0 0
633if lt fd 0
634ret 0
635end
636sz = fseek int fd 0 2
637if lt sz 0
638z = fclose int fd
639ret 0
640end
641z = fseek int fd 0 0
642clen = ulong sz
643er = enlarge rCorp add clen 1
644if lt er 0
645z = fclose int fd
646ret 0
647end
648rd = ulong 0
649while lt rd clen bound 100000000
650k = read int fd rCorp rd sub clen rd
651if le k 0
652break
653end
654rd = add rd ulong k
655end
656z = fclose int fd
657ret rd
658end
659
660fn rLoadPrm argi ulong to ulong
661pl = rLoadPath argi
662fd = fopen rPath 0 pl 0 0
663if lt fd 0
664ret 0
665end
666er = enlarge rPrm 16384
667if lt er 0
668z = fclose int fd
669ret 0
670end
671kn = read int fd rPrm 0 16383
672z = fclose int fd
673if le kn 0
674ret 0
675end
676ret ulong kn
677end
678
679fn rSaveOut argi ulong olen ulong to bool
680pl = rLoadPath argi
681fd = fopen rPath 0 pl 577 420
682if lt fd 0
683ret false
684end
685wr = ulong 0
686while lt wr olen bound 16384
687k = write int fd rOut wr sub olen wr
688if le k 0
689break
690end
691wr = add wr ulong k
692end
693z = fclose int fd
694ret true
695end
696
697fn rParseArgU argi ulong defv ulong to ulong
698tb = new uchar 32 0
699tl = argread argi tb 0 32
700if le tl 0
701ret defv
702end
703v = rParseU tb ulong tl
704if eq v 0
705ret defv
706end
707ret v
708end
709
710fn main to int
711ac = argcnt
712if lt ac 2
713rWrt 2 tUsage
714ret 2
715end
716m = new uchar 64 0
717ml = argread 1 m 0 64
718if le ml 0
719rWrt 2 tUsage
720ret 2
721end
722if rEqTxt m ulong ml tModeSelf
723ret rSelfcheck
724end
725if not rEqTxt m ulong ml tModeRet
726rWrt 2 tUsage
727ret 2
728end
729if lt ac 7
730rWrt 2 tUsage
731ret 2
732end
733rd = rLoadCorp 2
734if eq rd 0
735rWrt 2 tErrIo
736ret 1
737end
738qlen = rLoadPrm 3
739if eq qlen 0
740rWrt 2 tErrIo
741ret 1
742end
743topk = rParseArgU 4 2
744if gt topk 8
745topk = 2
746end
747maxb = rParseArgU 5 700
748put rSt 10 topk
749put rSt 11 maxb
750olen = rRunRetrieve rd qlen
751if not rSaveOut 6 olen
752rWrt 2 tErrIo
753ret 1
754end
755rWrt 1 tRetA
756rNum 1 topk
757rWrt 1 tRetB
758rNum 1 get rSt 8
759rWrt 1 tRetC
760rNum 1 get rSt 9
761rWrt 1 tRetD
762rNum 1 get rTopSc 0
763rWrt 1 tRetE
764rNum 1 olen
765rWrt 1 tNl
766ret 0
767end