interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / parity / pcmp.om

913 lines · 16301 bytes Raw Blame History
1note pcmp.om: parity comparator. usage: pcmp CORPUS ORACLE.out OMEGA.out REPORT SINCE
2note Reads the two answer streams written by pdrive for the same corpus and decides, record by
3note record, whether the Omega answer equals the oracle answer under the rule in spec/PARITY.md:
4note - same status line;
5note - same header lines as a multiset, ignoring `date`; `content-length` must also be equal unless
6note the body needed one of the masks below;
7note - same body bytes, except: an `elapsedMs` number; a `committedAt`/`createdAt`/`updatedAt` string
8note when both values are fresh (at or after SINCE, compared on the first 19 bytes) and have the
9note same shape (digits as 9, fraction ignored); and server-generated UUIDs, which must follow one
10note consistent one-to-one renaming learned only from `targetId`, receipt `id` and `spaceId`.
11note REPORT gets "PASS <index> <class>" or "FAIL <index> <class> <reason>" per record and a summary.
12note Exit 0 only when every record passes and both streams hold one record per corpus line.
13global pa uchar 4096
14global cb uchar 16777216
15global ra uchar 16777216
16global rs uchar 16777216
17global hb uchar 64
18global ob uchar 256
19global sn uchar 32
20note hl: header lines (offset, length) of the oracle (0..127) and Omega (128..255) answers
21global hl ulong 256
22global hu uchar 64
23note uuid renaming: mo (oracle) and ms (Omega) 36 bytes each, mc count
24global mo uchar 589824
25global ms uchar 589824
26note cv: 0 corpus len, 1 ofd, 2 sfd, 3 o pos, 4 s pos, 5 o len, 6 s len, 7 report fd, 8 masked,
27note 9 pass, 10 fail, 11 masks, 12 mc, 13 o err, 14 s err, 15 o body, 16 s body, 17 o hdrs, 18 s hdrs
28global cv ulong 24
29global ls ulong 1000000
30const NONE ulong 18446744073709551615
31text u_use "usage: pcmp CORPUS ORACLE.out OMEGA.out REPORT SINCE\n"
32text u_open "pcmp: cannot open a file\n"
33text u_big "pcmp: input too large\n"
34text r_pass "PASS "
35text r_fail "FAIL "
36text f_err " transport-error"
37text f_stat " status-line"
38text f_hdr " headers"
39text f_cl " content-length"
40text f_body " body@"
41text f_cnt " record-count"
42text s_tot "TOTAL records="
43text s_pass " pass="
44text s_fail " fail="
45text s_mask " masked-values="
46text s_map " renamed-uuids="
47text k_el "elapsedMs"
48text k_ca "committedAt"
49text k_cr "createdAt"
50text k_up "updatedAt"
51text k_ti "targetId"
52text k_id "id"
53text k_sp "spaceId"
54text k_rc "\"receipt\":{\"id\":"
55text k_rt "runtime"
56text v_rto "native-arm64-rust"
57text v_rts "native-arm64-omega"
58text h_date "date:"
59text h_cl "content-length:"
60
61fn say t view uchar * to void
62w = write 2 t 0 len t
63if lt w 0
64ret
65end
66end
67fn argp i ulong to ulong
68n = argread i pa 0 len pa
69if lt n 0
70ret 0
71end
72ret ulong n
73end
74fn opnr n ulong to int
75f = fopen pa 0 n 524288 0
76ret int f
77end
78fn rdall fd int to bool
79n = ulong 0
80while true bound 999999
81if ge n len cb
82say u_big
83ret false
84end
85r = pread fd cb n sub len cb n long n
86if lt r 0
87ret false
88end
89if eq r 0
90put cv 0 n
91ret true
92end
93n = add n ulong r
94end
95ret false
96end
97
98note ---- report output ----
99fn obt o ulong t view uchar * to ulong
100for i ulong len t
101put ob add o i get t i
102end
103ret add o len t
104end
105fn obdec o ulong v ulong to ulong
106k = o
107if ge v 10
108k = obdec o div v 10
109end
110put ob k uchar add rem v 10 48
111ret add k 1
112end
113fn flush n ulong to void
114put ob n 10
115w = write int get cv 7 ob 0 add n 1
116if lt w 0
117ret
118end
119end
120note " <class>" of corpus line ix (bytes up to the first tab), using the line starts in ls
121fn obcls o ulong ix ulong to ulong
122i = ulong 0
123if lt ix len ls
124i = get ls ix
125end
126put ob o 32
127r = add o 1
128for j ulong 40
129c = get cb add i j
130if or eq c 9 eq c 10
131break
132end
133put ob r c
134r = add r 1
135end
136ret r
137end
138
139note ---- answer records ----
140note header line of the next record at file offset p into hb; returns its length (0 = none)
141fn rhead fd int p ulong to ulong
142for i ulong 64
143put hb i 0
144end
145r = pread fd hb 0 64 long p
146if le r 0
147ret 0
148end
149for i ulong ulong r
150if eq get hb i 10
151ret add i 1
152end
153end
154ret 0
155end
156note byte count named in hb ("#<ix> <n>\n"); NONE for ERR
157fn hcount to ulong
158i = ulong 1
159while ne get hb i 32 bound 64
160i = add i 1
161end
162i = add i 1
163if eq get hb i 69
164ret NONE
165end
166v = ulong 0
167while ne get hb i 10 bound 64
168v = add mul v 10 ulong sub get hb i 48
169i = add i 1
170end
171ret v
172end
173fn rdo n ulong p ulong to bool
174k = ulong 0
175while lt k n bound 999999
176r = pread int get cv 1 ra k sub n k long add p k
177if le r 0
178ret false
179end
180k = add k ulong r
181end
182ret true
183end
184fn rds n ulong p ulong to bool
185k = ulong 0
186while lt k n bound 999999
187r = pread int get cv 2 rs k sub n k long add p k
188if le r 0
189ret false
190end
191k = add k ulong r
192end
193ret true
194end
195note load the next oracle record; 0 none left, 1 loaded, 2 ERR record, 3 bad stream
196fn nexto to ulong
197p = get cv 3
198h = rhead int get cv 1 p
199if eq h 0
200ret 0
201end
202n = hcount
203if eq n NONE
204put cv 3 add p h
205put cv 5 0
206ret 2
207end
208if gt n len ra
209ret 3
210end
211if not rdo n add p h
212ret 3
213end
214put cv 3 add add p h add n 1
215put cv 5 n
216ret 1
217end
218fn nexts to ulong
219p = get cv 4
220h = rhead int get cv 2 p
221if eq h 0
222ret 0
223end
224n = hcount
225if eq n NONE
226put cv 4 add p h
227put cv 6 0
228ret 2
229end
230if gt n len rs
231ret 3
232end
233if not rds n add p h
234ret 3
235end
236put cv 4 add add p h add n 1
237put cv 6 n
238ret 1
239end
240
241note ---- head comparison ----
242note split the oracle answer: header lines into hl[0..), count cv 17, body start cv 15
243fn splito to void
244n = get cv 5
245put cv 17 0
246put cv 15 n
247s = ulong 0
248c = ulong 0
249for i ulong n
250if and eq get ra i 13 eq get ra add i 1 10
251if eq i s
252put cv 15 add i 2
253break
254end
255if and gt s 0 lt c 63
256put hl mul c 2 s
257put hl add mul c 2 1 sub i s
258c = add c 1
259end
260s = add i 2
261end
262end
263put cv 17 c
264end
265fn splits to void
266n = get cv 6
267put cv 18 0
268put cv 16 n
269s = ulong 0
270c = ulong 0
271for i ulong n
272if and eq get rs i 13 eq get rs add i 1 10
273if eq i s
274put cv 16 add i 2
275break
276end
277if and gt s 0 lt c 63
278put hl add 128 mul c 2 s
279put hl add 128 add mul c 2 1 sub i s
280c = add c 1
281end
282s = add i 2
283end
284end
285put cv 18 c
286end
287note status lines equal
288fn stateq to bool
289for i ulong get cv 5
290if ne get ra i get rs i
291ret false
292end
293if eq get ra i 13
294ret true
295end
296end
297ret false
298end
299fn ohas p ulong t view uchar * to bool
300for i ulong len t
301if ne get ra add p i get t i
302ret false
303end
304end
305ret true
306end
307fn shas p ulong t view uchar * to bool
308for i ulong len t
309if ne get rs add p i get t i
310ret false
311end
312end
313ret true
314end
315note oracle header line i equals Omega header line j
316fn lneq i ulong j ulong to bool
317oo = get hl mul i 2
318ol = get hl add mul i 2 1
319so = get hl add 128 mul j 2
320if ne ol get hl add 128 add mul j 2 1
321ret false
322end
323for k ulong ol
324if ne get ra add oo k get rs add so k
325ret false
326end
327end
328ret true
329end
330note skip-class of a header: 0 compare, 1 date (ignored), 2 content-length
331fn okind i ulong to ulong
332p = get hl mul i 2
333if ohas p h_date
334ret 1
335end
336if ohas p h_cl
337ret 2
338end
339ret 0
340end
341fn skind j ulong to ulong
342p = get hl add 128 mul j 2
343if shas p h_date
344ret 1
345end
346if shas p h_cl
347ret 2
348end
349ret 0
350end
351note 0 equal, 1 headers differ, 2 content-length differs
352fn hdreq to ulong
353no = get cv 17
354ns = get cv 18
355for j ulong 64
356put hu j 0
357end
358for j ulong ns
359if eq skind j 1
360put hu j 1
361end
362end
363r = ulong 0
364for i ulong no
365ki = okind i
366if eq ki 1
367continue
368end
369f = false
370for j ulong ns
371if and eq get hu j 0 lneq i j
372put hu j 1
373f = true
374break
375end
376end
377if not f
378if eq ki 2
379r = 2
380continue
381end
382ret 1
383end
384end
385ret hdrrest r
386end
387note every Omega line must have been matched (content-length mismatches report 2)
388fn hdrrest r ulong to ulong
389for j ulong get cv 18
390if eq get hu j 0
391if ne skind j 2
392ret 1
393end
394ret 2
395end
396end
397ret r
398end
399
400note ---- body comparison with masks ----
401note key ending just before oracle offset p (p points at the byte after `":`) equals t
402fn keyis p ulong t view uchar * to bool
403n = len t
404if lt p add n 3
405ret false
406end
407s = sub sub p 2 n
408if ne get ra sub s 1 34
409ret false
410end
411for i ulong n
412if ne get ra add s i get t i
413ret false
414end
415end
416ret true
417end
418note oracle offset of the value start that contains the mismatch at io, or NONE (the scan stays
419note after the last resync point lo)
420fn valat io ulong lo ulong to ulong
421k = io
422while gt k lo bound 999999
423if and eq get ra sub k 1 58 eq get ra sub k 2 34
424ret k
425end
426c = get ra sub k 1
427opn = false
428if and eq c 34 gt sub k 1 lo
429opn = eq get ra sub k 2 58
430end
431if and not opn or eq c 44 or eq c 123 eq c 34
432if ne k io
433ret NONE
434end
435end
436k = sub k 1
437end
438ret NONE
439end
440fn oend p ulong n ulong str bool to ulong
441for j ulong sub n p
442c = get ra add p j
443if str
444if eq c 34
445ret add p j
446end
447else
448if or eq c 44 or eq c 125 eq c 93
449ret add p j
450end
451end
452end
453ret n
454end
455fn send2 p ulong n ulong str bool to ulong
456for j ulong sub n p
457c = get rs add p j
458if str
459if eq c 34
460ret add p j
461end
462else
463if or eq c 44 or eq c 125 eq c 93
464ret add p j
465end
466end
467end
468ret n
469end
470note fresh timestamp: at least SINCE on the first 19 bytes, and the same shape in both
471fn tseq a ulong ae ulong b ulong be ulong to bool
472if or lt sub ae a 19 lt sub be b 19
473ret false
474end
475for i ulong 19
476if ne isdig get ra add a i isdig get rs add b i
477ret false
478end
479end
480ret and tfresh a true tfresh b false
481end
482fn isdig c uchar to bool
483ret and ge c 48 le c 57
484end
485fn tfresh p ulong o bool to bool
486for i ulong 19
487c = get rs add p i
488if o
489c = get ra add p i
490end
491s = get sn i
492if gt c s
493ret true
494end
495if lt c s
496ret false
497end
498end
499ret true
500end
501fn uuidok p ulong o bool to bool
502for i ulong 36
503c = get rs add p i
504if o
505c = get ra add p i
506end
507if or eq i 8 or eq i 13 or eq i 18 eq i 23
508if ne c 45
509ret false
510end
511continue
512end
513if not or isdig c and ge c 97 le c 102
514ret false
515end
516end
517ret true
518end
519fn meqo m ulong a ulong to bool
520b = mul m 36
521for i ulong 36
522if ne get mo add b i get ra add a i
523ret false
524end
525end
526ret true
527end
528fn meqs m ulong a ulong to bool
529b = mul m 36
530for i ulong 36
531if ne get ms add b i get rs add a i
532ret false
533end
534end
535ret true
536end
537note renaming lookup: 1 pair known, 2 conflict, 0 both unknown
538fn mfind a ulong b ulong to ulong
539for m ulong get cv 12
540fo = meqo m a
541fs = meqs m b
542if and fo fs
543ret 1
544end
545if or fo fs
546ret 2
547end
548end
549ret 0
550end
551fn madd a ulong b ulong to bool
552m = get cv 12
553if ge m 16384
554ret false
555end
556for i ulong 36
557put mo add mul m 36 i get ra add a i
558put ms add mul m 36 i get rs add b i
559end
560put cv 12 add m 1
561ret true
562end
563note uuid pair at oracle a / Omega b may differ: learnable keys add a renaming
564fn uuideq a ulong b ulong learn bool to bool
565if not and uuidok a true uuidok b false
566ret false
567end
568f = mfind a b
569if eq f 1
570ret true
571end
572if eq f 2
573ret false
574end
575if not learn
576ret false
577end
578ret madd a b
579end
580note the one /health field that differs by design (spec/PARITY.md): runtime is native-arm64-rust in the
581note oracle and native-arm64-omega here, exactly
582fn rtmask a ulong ae ulong b ulong be ulong to bool
583if or ne sub ae a len v_rto ne sub be b len v_rts
584ret false
585end
586for i ulong len v_rto
587if ne get ra add a i get v_rto i
588ret false
589end
590end
591for i ulong len v_rts
592if ne get rs add b i get v_rts i
593ret false
594end
595end
596ret true
597end
598note decimal value of a content-length header line at p (length n) of the oracle (o) or Omega answer
599fn clval p ulong n ulong o bool to ulong
600v = ulong 0
601for i ulong sub n len h_cl
602c = get rs add add p len h_cl i
603if o
604c = get ra add add p len h_cl i
605end
606if isdig c
607v = add mul v 10 sub ulong c 48
608end
609end
610ret v
611end
612note HEAD answers carry no body, so a masked value shows only in content-length: the runtime value
613note of /health and /status makes Omega's exactly one larger; the elapsedMs number of a search or
614note recall answer may differ in length by less than 24 bytes (the longest JSON f64 text,
615note -2.2250738585072014e-308, is 24 bytes). Accepted only when both bodies are empty.
616fn headcl ix ulong to bool
617if or ne get cv 15 get cv 5 ne get cv 16 get cv 6
618ret false
619end
620vo = NONE
621vs = NONE
622for i ulong get cv 17
623if eq okind i 2
624vo = clval get hl mul i 2 get hl add mul i 2 1 true
625end
626end
627for j ulong get cv 18
628if eq skind j 2
629p = get hl add 128 mul j 2
630n = get hl add add 128 mul j 2 1
631vs = clval p n false
632end
633end
634if or eq vo NONE eq vs NONE
635ret false
636end
637if eq vs add vo 1
638ret true
639end
640if not hdtim ix
641ret false
642end
643if gt vo vs
644ret le sub vo vs 24
645end
646ret le sub vs vo 24
647end
648text t_hs "HEAD\t/api/cortex/search"
649text t_hr "HEAD\t/api/cortex/recall"
650note corpus line ix is a HEAD of /api/cortex/search or /api/cortex/recall (answers with elapsedMs)
651fn hdtim ix ulong to bool
652if ge ix len ls
653ret false
654end
655i = get ls ix
656for j ulong 64
657if eq get cb add i j 9
658ret or cbhas add add i j 1 t_hs cbhas add add i j 1 t_hr
659end
660end
661ret false
662end
663fn cbhas p ulong t view uchar * to bool
664for k ulong len t
665if ne get cb add p k get t k
666ret false
667end
668end
669ret true
670end
671note one masked value at oracle vo / Omega vs; returns false when the mismatch is not maskable;
672note on success cv 19 / cv 20 hold the oracle / Omega offsets after the value
673fn maskv vo ulong vs ulong to bool
674no = get cv 5
675ns = get cv 6
676if keyis vo k_el
677put cv 19 oend vo no false
678put cv 20 send2 vs ns false
679ret true
680end
681if or ne get ra vo 34 ne get rs vs 34
682ret false
683end
684ae = oend add vo 1 no true
685be = send2 add vs 1 ns true
686put cv 19 add ae 1
687put cv 20 add be 1
688a = add vo 1
689b = add vs 1
690if keyis vo k_rt
691ret rtmask a ae b be
692end
693if or keyis vo k_ca or keyis vo k_cr keyis vo k_up
694ret tseq a ae b be
695end
696if or ne sub ae a 36 ne sub be b 36
697ret false
698end
699rc = false
700if ge vo len k_rc
701rc = ohas sub vo len k_rc k_rc
702end
703lr = or rc or keyis vo k_ti keyis vo k_sp
704ret uuideq a b lr
705end
706note count of equal bytes of ra from io and rs from is (c at most 65536), then the chunked run
707fn eqchunk io ulong is ulong c ulong to ulong
708for j ulong c
709if ne get ra add io j get rs add is j
710ret j
711end
712end
713ret c
714end
715fn eqrun io ulong is ulong no ulong ns ulong to ulong
716lim = sub no io
717if lt sub ns is lim
718lim = sub ns is
719end
720r = ulong 0
721while lt r lim bound 999999
722c = sub lim r
723if gt c 65536
724c = 65536
725end
726m = eqchunk add io r add is r c
727r = add r m
728if lt m c
729break
730end
731end
732ret r
733end
734note 0 equal (cv 8 = 1 when a mask was used), else 1 + oracle offset of the first difference
735fn bodyeq to ulong
736io = get cv 15
737is = get cv 16
738no = get cv 5
739ns = get cv 6
740lo = io
741put cv 8 0
742while true bound 999999
743if or ge io no ge is ns
744if and eq io no eq is ns
745ret 0
746end
747ret add io 1
748end
749m = eqrun io is no ns
750io = add io m
751is = add is m
752if or ge io no ge is ns
753continue
754end
755vo = valat io lo
756if eq vo NONE
757ret add io 1
758end
759vs = sub is sub io vo
760if not maskv vo vs
761ret add io 1
762end
763put cv 8 1
764put cv 11 add get cv 11 1
765io = get cv 19
766is = get cv 20
767lo = io
768end
769ret add io 1
770end
771
772note ---- per record ----
773fn verdict ix ulong t view uchar * extra ulong to void
774o = obt 0 r_fail
775o = obcls obdec o ix ix
776o = obt o t
777if ne extra NONE
778o = obdec o extra
779end
780flush o
781put cv 10 add get cv 10 1
782end
783fn passed ix ulong to void
784o = obt 0 r_pass
785o = obcls obdec o ix ix
786flush o
787put cv 9 add get cv 9 1
788end
789fn onerec ix ulong a ulong b ulong to void
790if or ne a 1 ne b 1
791verdict ix f_err NONE
792ret
793end
794splito
795splits
796if not stateq
797verdict ix f_stat NONE
798ret
799end
800h = hdreq
801if eq h 1
802verdict ix f_hdr NONE
803ret
804end
805d = bodyeq
806if ne d 0
807verdict ix f_body sub d get cv 15
808ret
809end
810if eq h 2
811if or ne get cv 8 0 headcl ix
812passed ix
813ret
814end
815verdict ix f_cl NONE
816ret
817end
818passed ix
819end
820note loops run in 65536-byte chunks: one loop may pass at most 1000000 times (trap 4 beyond)
821fn linesb b ulong e ulong k0 ulong to ulong
822k = k0
823for j ulong sub e b
824if eq get cb add b j 10
825k = add k 1
826if lt k len ls
827put ls k add add b j 1
828end
829end
830end
831ret k
832end
833fn lines to ulong
834k = ulong 0
835n = get cv 0
836put ls 0 0
837b = ulong 0
838while lt b n bound 999999
839e = add b 65536
840if gt e n
841e = n
842end
843k = linesb b e k
844b = e
845end
846ret k
847end
848fn summary nl ulong ix ulong to void
849o = obdec obt 0 s_tot ix
850o = obdec obt o s_pass get cv 9
851o = obdec obt o s_fail get cv 10
852o = obdec obt o s_mask get cv 11
853o = obdec obt o s_map get cv 12
854flush o
855if ne nl ix
856flush obt 0 f_cnt
857put cv 10 add get cv 10 1
858end
859end
860fn runall to int
861nl = lines
862ix = ulong 0
863while true bound 999999
864a = nexto
865b = nexts
866if and eq a 0 eq b 0
867break
868end
869if or eq a 3 eq b 3
870say u_big
871ret 3
872end
873onerec ix a b
874ix = add ix 1
875end
876summary nl ix
877if ne get cv 10 0
878ret 1
879end
880ret 0
881end
882fn main to int
883if ne ulong argcnt 6
884say u_use
885ret 2
886end
887fd = opnr argp 1
888if lt fd 0
889say u_open
890ret 3
891end
892if not rdall fd
893ret 3
894end
895x = fclose fd
896put cv 1 ulong opnr argp 2
897put cv 2 ulong opnr argp 3
898n = argp 4
899f = fopen pa 0 n 524865 420
900put cv 7 ulong f
901if or lt int get cv 1 0 or lt int get cv 2 0 lt f 0
902say u_open
903ret 3
904end
905if ne argp 5 19
906say u_use
907ret 2
908end
909for i ulong 19
910put sn i get pa i
911end
912ret runall
913end