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 / node / tests / tool-replay.om

1117 lines · 28846 bytes Raw Blame History
1note seed/node/tests/tool-replay.om: replay the round-40 tool transcripts through the Omega Node tool
2note and compare every next prompt byte for byte (the seeded-Omega port of tool-replay.py).
3note Build: { printf 'om2\n'; cat seed/json/json.om seed/node/tests/drv.om seed/node/tests/tool-replay.om; }
4note | seed/out/omc > build/node/tool-replay
5note Usage: tool-replay TOOL OMC ROUND40 OUTDIR. Reads ROUND40/final-selected.json (a list of runs).
6note Per run: OUTDIR/<job.id> is removed, ROOT/dirname(job.path) and ROOT/.work made, job.broken written
7note to ROOT/job.path unless job.fault is "missing". For every turn but the last the turn's raw answer
8note is cut at the first "\nDone.", stripped (Python str.strip whitespace) and parsed as JSON; its tool
9note runs as one TOOL process (read_file: read ROOT PATH; omega_check: check ROOT PATH ROOT/.work OMC
10note KIND K N A with KIND add 1, mul 2, loop 3 and N job.n or 0; any other tool: replace ROOT PATH OLD
11note NEW), stdout captured to OUTDIR/.tool-replay.out, stderr to /dev/null, 120 s deadline. Exit 0 gives
12note the OMNT1 record (status, observation, content; content dropped for status 1); another exit gives
13note status None and observation "exit N" (N = -signal for a signal). The rebuilt repair.py prompt
14note (Task, File, Goal, State, Source, Tool result, Answer) must equal the next turn's prompt; the
15note first difference prints "MISMATCH id fault tool status ST REPR" and " want REPR" (Python repr,
16note first and last 200 code points) and fails the run. Summary: TOOL-REPLAY runs= pass= fail=
17note actions=; exit 0 when no run failed, 1 otherwise, 2 for bad usage or an unreadable fixture.
18note Where tool-replay.py would have raised (missing or mistyped fixture field, style outside 0..50,
19note non-integer number, tool timeout, malformed OMNT1 record, field over its buffer), this driver
20note fails the run instead: an "ERROR id step" line, or a MISMATCH with status None and observation
21note "timeout", "malformed record" or "spawn error". A job id that is empty, ".", ".." or holds '/'
22note is refused (ERROR) so OUTDIR/<id> is always a direct child of OUTDIR. repr approximation: code
23note points U+0080..U+00A0 and U+00AD print as \xNN, every other non-ASCII code point raw.
24note Buffers: raw answer 8192 bytes, prompt and tool stdout 16384, id 1024, path 3072, fault 1024,
25note tool name 1024, broken 6144, any path 4095.
26note ba (32768 bytes): TOOL 0, OMC 4096, OUTDIR 8192, ROOT 12288, WORK 16384, capture file 20480,
27note prompt head 24576, fixture path 28672; their lengths in tra[0..7].
28note bj (16384 bytes): id 0, path 1024, fault 4096, tool name 5120, broken 6144, packed argv 12288;
29note lengths tra-style in trb: [0] id, [1] path, [2] fault, [3] broken, [4] tool name, [5] broken set.
30
31global tra ulong 8
32global trb ulong 8
33note trv: [0] style, [1] kind code (1 add, 2 mul, 3 loop, 0 other), [2] k, [3] n, [4] a, [5] n given.
34global trv long 8
35note trs: [0] status (dvnone = None), [1] obs offset, [2] obs length, [3] content offset,
36note [4] content length, [5] next-turn token, [6] packed argv length.
37global trs ulong 8
38note trn: [0] runs, [1] passed runs, [2] failed runs, [3] tool actions.
39global trn ulong 4
40
41text trkjob "job"
42text trkid "id"
43text trkpath "path"
44text trkflt "fault"
45text trkkind "kind"
46text trkstyl "style"
47text trkk "k"
48text trkn "n"
49text trka "a"
50text trkbrok "broken"
51text trkturn "turns"
52text trkraw "raw"
53text trkstat "state"
54text trkprom "prompt"
55text trktool "tool"
56text trkold "old"
57text trknew "new"
58text trmiss "missing"
59text trrf "read_file"
60text troc "omega_check"
61text trcread "read"
62text trcrepl "replace"
63text trcchk "check"
64text trloop "loop"
65text tradd "add"
66text trmul "mul"
67text trdone "\nDone."
68text trwork ".work"
69text trcap ".tool-replay.out"
70text trsel "/final-selected.json"
71text trnull "/dev/null"
72text trmagic "OMNT1"
73text trtask "Task: "
74text trfile "\nFile: "
75text trgoal "\nGoal: "
76text trstate "\nState: "
77text trg1 "calc starts with x and adds "
78text trg2 " exactly "
79text trg3 " times in a for loop. main calls calc with "
80text trg4 "calc returns x "
81text trplus "plus "
82text trtimes "times "
83text trg5 ". main calls calc with "
84text trdot "."
85text trsrc "Source:\n"
86text trres "Tool result: "
87text trans "Answer:\n"
88text trmis "MISMATCH "
89text trstw " status "
90text trnone "None"
91text trwant " want "
92text trsum1 "TOOL-REPLAY runs="
93text trsum2 " pass="
94text trsum3 " fail="
95text trsum4 " actions="
96text trexit "exit "
97text trtmo "timeout"
98text trbadr "malformed record"
99text trspawn "spawn error"
100text trerr "ERROR "
101text trejob "job fields"
102text trepath "paths"
103text tretree "tree setup"
104text treturn "turns"
105text treraw "raw"
106text trejson "raw json"
107text tretool "tool"
108text treargv "arguments"
109text trewant "next prompt"
110text treload "tool-replay: cannot load ROUND40/final-selected.json\n"
111text trusage "usage: tool-replay TOOL OMC ROUND40 OUTDIR\n"
112note trsty1, trsty2: repair.py STYLES 0..25 and 26..50, one per line.
113text trsty1 "Repair this Omega file and verify it.\nUse tools to fix this Omega program, then test it.\nCheck this Omega file, correct any fault and confirm the result.\nPlease get this Omega file working according to its goal and check your work.\nInvestigate this Omega file and make its behavior match the goal.\nDiagnose this Omega file, repair it as needed, and verify the result.\nRepair the calculation in this Omega file and verify the result.\nFind and repair the mistake in this Omega file, then confirm its behavior.\nMake this Omega file satisfy its stated goal and verify the repaired result.\nFix this Omega file.\nCheck and repair the Omega program.\nMake the program match its goal and test it.\nUse tools to repair the file and check it.\nFind the wrong calculation, fix it, and test it.\nInspect this program and fix the error.\nRead the file, check it, repair it, and test again.\nPlease correct the Omega calculation and verify it.\nRepair this file to meet the stated goal.\nWork through this Omega repair and check the result.\nCheck this Omega file, find the error, repair it and verify the result.\nPlease inspect this Omega program to repair the calculation and verify the result.\nUse the goal to fix this Omega file and verify the changes.\nPlease repair this Omega program to do what the goal asks.\nGet this Omega file to match the goal, then check the result.\nPlease carefully repair the calculation using tools and confirm that the tests pass.\nPlease carefully examine this Omega file and repair any calculation that does not match its goal.\n"
114text trsty2 "Find the incorrect behavior in this Omega file, fix it, and test the result.\nInspect and repair this Omega file, then verify it.\nCorrect this Omega program according to its goal.\nFix the error in this Omega program and check the result.\nCheck this file and repair it to meet its goal.\nPlease fix this program to match its goal.\nCorrect the calculation in this Omega file.\nRead this program and repair its calculation.\nPlease check the code and fix the mistake.\nRepair the program according to the stated goal.\nUse the goal to correct this Omega calculation.\nFix the calculation and test the corrected program.\nPlease inspect the file and correct the error.\nCheck the calculation, make the repair, and verify it.\nMake this file behave as the goal describes.\nPlease correct this program and check your work.\nRepair this calculation, then confirm the result.\nRepair this file and confirm it meets the goal.\nCheck the Omega program and correct its calculation.\nInspect this file, fix its calculation, and verify it.\nMake this Omega program meet the goal, then test it.\nRepair the file and verify its goal.\nCheck the code, repair its fault, and test it.\nFix the error in this file and confirm its goal.\nInspect the code and repair it to satisfy the goal.\n"
115
116note ======== Python str.strip whitespace and repr
117
118note tr_cpn c: the byte length of the UTF-8 sequence led by c (1 for ASCII or a stray byte).
119fn tr_cpn c uchar to ulong
120if lt c 192
121ret 1
122end
123if lt c 224
124ret 2
125end
126if lt c 240
127ret 3
128end
129if lt c 248
130ret 4
131end
132ret 1
133end
134
135note tr_cp b i k: the code point of the k-byte (2..4) UTF-8 sequence at b[i].
136fn tr_cp b view uchar 8192 i ulong k ulong to ulong
137c = and ulong get b i shr 127 k
138j = ulong 1
139while lt j k bound 4
140d = and ulong get b add i j 63
141c = or shl c 6 d
142j = add j 1
143end
144ret c
145end
146
147note tr_uws cp: whether non-ASCII code point cp is Python whitespace (str.isspace).
148fn tr_uws cp ulong to bool
149if or eq cp 133 eq cp 160
150ret true
151end
152if eq cp 5760
153ret true
154end
155if and ge cp 8192 le cp 8202
156ret true
157end
158if or eq cp 8232 eq cp 8233
159ret true
160end
161if or eq cp 8239 eq cp 8287
162ret true
163end
164ret eq cp 12288
165end
166
167note tr_aws c: whether byte c is ASCII Python whitespace (\t \n \v \f \r, 0x1c..0x1f, space).
168fn tr_aws c uchar to bool
169ret or and ge c 9 le c 13 or eq c 32 and ge c 28 le c 31
170end
171
172note tr_wsat b i e: the byte length of the whitespace code point at b[i] (ending by e), or 0.
173fn tr_wsat b view uchar 8192 i ulong e ulong to ulong
174c = get b i
175if tr_aws c
176ret 1
177end
178k = tr_cpn c
179if eq k 1
180ret 0
181end
182if gt add i k e
183ret 0
184end
185if tr_uws tr_cp b i k
186ret k
187end
188ret 0
189end
190
191note tr_wsend b s e: the byte length of the whitespace code point ending at b[e] (e > s), or 0.
192fn tr_wsend b view uchar 8192 s ulong e ulong to ulong
193c = get b sub e 1
194if lt c 128
195if tr_aws c
196ret 1
197end
198ret 0
199end
200j = sub e 1
201while gt j s bound 4
202d = get b j
203if or lt d 128 gt d 191
204break
205end
206if ge sub e j 4
207break
208end
209j = sub j 1
210end
211k = tr_cpn get b j
212if eq k 1
213ret 0
214end
215if ne add j k e
216ret 0
217end
218if tr_uws tr_cp b j k
219ret k
220end
221ret 0
222end
223
224note tr_text rb n ib: raw.split('\nDone.')[0].strip() of rb[0, n) copied to ib[0, ..); its length.
225fn tr_text rb view uchar 8192 n ulong ib edit uchar 8192 to ulong
226e = dv_find part rb 0 n trdone 0
227if eq e dvnone
228e = n
229end
230s = ulong 0
231while lt s e bound 8193
232k = tr_wsat rb s e
233if eq k 0
234break
235end
236s = add s k
237end
238while gt e s bound 8193
239k = tr_wsend rb s e
240if eq k 0
241break
242end
243e = sub e k
244end
245q = dv_cat ib 0 part rb s sub e s
246ret sub e s
247end
248
249note tr_quote b: the quote Python repr uses for b: '"' when b holds a ' and no ", else '.
250fn tr_quote b view uchar * to uchar
251s1 = false
252d1 = false
253for i ulong len b
254c = get b i
255if eq c 39
256s1 = true
257end
258if eq c 34
259d1 = true
260end
261end
262if and s1 not d1
263ret 34
264end
265ret 39
266end
267
268note tr_rlen b i: the input bytes one repr step consumes at b[i] (a whole UTF-8 sequence).
269fn tr_rlen b view uchar * i ulong to ulong
270c = get b i
271if lt c 128
272ret 1
273end
274k = tr_cpn c
275if gt k sub len b i
276ret 1
277end
278ret k
279end
280
281note tr_hex t w c: "\xNN" (lower-case hex of c) at t[w]; w + 4.
282fn tr_hex t edit uchar 65600 w ulong c uchar to ulong
283put t w 92
284put t add w 1 120
285h = shr c 4
286l = and c 15
287if lt h 10
288put t add w 2 add h 48
289else
290put t add w 2 add h 87
291end
292if lt l 10
293put t add w 3 add l 48
294else
295put t add w 3 add l 87
296end
297ret add w 4
298end
299
300note tr_esc t w c: a backslash and c at t[w]; w + 2.
301fn tr_esc t edit uchar 65600 w ulong c uchar to ulong
302put t w 92
303put t add w 1 c
304ret add w 2
305end
306
307note tr_r1 t w b i q: the repr of the code point at b[i] (quote q) written at t[w]; the new w.
308fn tr_r1 t edit uchar 65600 w ulong b view uchar * i ulong q uchar to ulong
309c = get b i
310if or eq c 92 eq c q
311ret tr_esc t w c
312end
313if eq c 9
314ret tr_esc t w 116
315end
316if eq c 10
317ret tr_esc t w 110
318end
319if eq c 13
320ret tr_esc t w 114
321end
322if or lt c 32 eq c 127
323ret tr_hex t w c
324end
325if lt c 128
326put t w c
327ret add w 1
328end
329k = tr_rlen b i
330if eq k 2
331if eq c 194
332d = get b add i 1
333if or le d 160 eq d 173
334ret tr_hex t w d
335end
336end
337end
338for j ulong k
339put t add w j get b add i j
340end
341ret add w k
342end
343
344note tr_rfull t b: Python repr of the string b (UTF-8) written to t[0, ..); its length.
345fn tr_rfull t edit uchar 65600 b view uchar * to ulong
346q = tr_quote b
347put t 0 q
348w = ulong 1
349i = ulong 0
350while lt i len b bound 16777217
351w = tr_r1 t w b i q
352i = add i tr_rlen b i
353end
354put t w q
355ret add w 1
356end
357
358note tr_rcut t n lim: the byte length of the first lim code points of t[0, n).
359fn tr_rcut t view uchar 65600 n ulong lim ulong to ulong
360c = ulong 0
361i = ulong 0
362while lt i n bound 65601
363d = get t i
364if or lt d 128 gt d 191
365if eq c lim
366ret i
367end
368c = add c 1
369end
370i = add i 1
371end
372ret n
373end
374
375note tr_repr o p b lim: repr(b)[:lim] appended at o[p] (dv_cat rules); the new end or dvnone.
376note b is at most 16384 bytes.
377fn tr_repr o edit uchar * p ulong b view uchar * lim ulong to ulong
378t = new uchar 65600 0
379n = tr_rfull t b
380m = tr_rcut t n lim
381ret dv_cat o p view part t 0 m
382end
383
384note tr_back b n k: the start of the last k code points of b[0, n) (0 when fewer).
385fn tr_back b view uchar 16384 n ulong k ulong to ulong
386i = n
387c = ulong 0
388while gt i 0 bound 16385
389i = sub i 1
390d = get b i
391if or lt d 128 gt d 191
392c = add c 1
393if eq c k
394ret i
395end
396end
397end
398ret 0
399end
400
401note ======== the job and its prompt head
402
403note tr_line o p t k: line k of the newline-terminated lines t appended at o[p]; the end or dvnone.
404fn tr_line o edit uchar * p ulong t view uchar * k ulong to ulong
405i = ulong 0
406c = ulong 0
407s = ulong 0
408while lt i len t bound 4096
409if eq get t i 10
410if eq c k
411ret dv_cat o p part t s sub i s
412end
413c = add c 1
414s = add i 1
415end
416i = add i 1
417end
418ret dvnone
419end
420
421note tr_style o p: STYLES[job.style] appended at o[p]; dvnone for a style outside 0..50.
422fn tr_style o edit uchar * p ulong to ulong
423s = get trv 0
424if or lt s 0 gt s 50
425ret dvnone
426end
427if lt s 26
428ret tr_line o p trsty1 ulong s
429end
430ret tr_line o p trsty2 sub ulong s 26
431end
432
433note tr_goal o p: repair.py goal(job) appended at o[p]; the end or dvnone.
434fn tr_goal o edit uchar * p ulong to ulong
435if eq get trv 1 3
436if eq get trv 5 0
437ret dvnone
438end
439q = dv_cat o p trg1
440q = dv_fmtl o q get trv 2
441q = dv_cat o q trg2
442q = dv_fmtl o q get trv 3
443q = dv_cat o q trg3
444q = dv_fmtl o q get trv 4
445ret dv_cat o q trdot
446end
447q = dv_cat o p trg4
448if eq get trv 1 1
449q = dv_cat o q trplus
450else
451q = dv_cat o q trtimes
452end
453q = dv_fmtl o q get trv 2
454q = dv_cat o q trg5
455q = dv_fmtl o q get trv 4
456ret dv_cat o q trdot
457end
458
459note tr_jint src tape v i: whether token v is an integer; stores it in trv[i].
460fn tr_jint src view uchar * tape view uint dvtcap v ulong i ulong to bool
461if not dv_jisl src tape v
462ret false
463end
464put trv i dv_jnum src tape v
465ret true
466end
467
468note tr_jnums src tape j: job style, kind, k, a and n (default 0) into trv; 0 or -1.
469fn tr_jnums src view uchar * tape view uint dvtcap j ulong to long
470if not tr_jint src tape dv_jkey src tape j trkstyl 0
471ret neg long 1
472end
473if not tr_jint src tape dv_jkey src tape j trkk 2
474ret neg long 1
475end
476if not tr_jint src tape dv_jkey src tape j trka 4
477ret neg long 1
478end
479put trv 3 0
480put trv 5 0
481nv = dv_jkey src tape j trkn
482if ne nv jsnone
483if not tr_jint src tape nv 3
484ret neg long 1
485end
486put trv 5 1
487end
488v = dv_jkey src tape j trkkind
489put trv 1 0
490if dv_jsteq src tape v tradd
491put trv 1 1
492end
493if dv_jsteq src tape v trmul
494put trv 1 2
495end
496if dv_jsteq src tape v trloop
497put trv 1 3
498end
499ret 0
500end
501
502note tr_idok b: 0 when job id b is a single non-empty path component other than "." and "..".
503fn tr_idok b view uchar * to long
504n = len b
505if eq n 0
506ret neg long 1
507end
508for i ulong n
509if or eq get b i 47 eq get b i 0
510ret neg long 1
511end
512end
513if eq get b 0 46
514if eq n 1
515ret neg long 1
516end
517if eq n 2
518if eq get b 1 46
519ret neg long 1
520end
521end
522end
523ret 0
524end
525
526note tr_job src tape j bj: job j's id, path, fault, broken (unless fault is "missing") into bj and
527note its numbers into trv; 0 or -1.
528fn tr_job src view uchar * tape view uint dvtcap j ulong bj edit uchar 16384 to long
529v = dv_jkey src tape j trkid
530n = dv_jstr src tape v part bj 0 1024
531if lt n 0
532ret neg long 1
533end
534put trb 0 ulong n
535v = dv_jkey src tape j trkpath
536n = dv_jstr src tape v part bj 1024 3072
537if lt n 0
538ret neg long 1
539end
540put trb 1 ulong n
541v = dv_jkey src tape j trkflt
542n = dv_jstr src tape v part bj 4096 1024
543if lt n 0
544ret neg long 1
545end
546put trb 2 ulong n
547if lt tr_jnums src tape j 0
548ret neg long 1
549end
550if not dv_eq view part bj 4096 get trb 2 trmiss
551v = dv_jkey src tape j trkbrok
552n = dv_jstr src tape v part bj 6144 6144
553if lt n 0
554ret neg long 1
555end
556put trb 3 ulong n
557put trb 5 1
558end
559ret tr_idok view part bj 0 get trb 0
560end
561
562note tr_head ba bj: "Task: STYLE\nFile: PATH\nGoal: GOAL\nState: " into ba[24576, ..); 0 or -1.
563fn tr_head ba edit uchar 32768 bj view uchar 16384 to long
564o = new uchar 4096 0
565p = dv_cat o 0 trtask
566p = tr_style o p
567p = dv_cat o p trfile
568p = dv_cat o p part bj 1024 get trb 1
569p = dv_cat o p trgoal
570p = tr_goal o p
571p = dv_cat o p trstate
572if eq p dvnone
573ret neg long 1
574end
575q = dv_cat part ba 24576 4096 0 view part o 0 p
576put tra 6 p
577ret 0
578end
579
580note tr_paths ba bj: ROOT = OUTDIR/id and WORK = ROOT/.work into ba, then the head; 0 or -1.
581fn tr_paths ba edit uchar 32768 bj view uchar 16384 to long
582t = new uchar 4096 0
583n = dv_cat t 0 view part ba 8192 get tra 2
584ni = get trb 0
585r = dv_join part ba 12288 4096 view part t 0 n part bj 0 ni
586if eq r dvnone
587ret neg long 1
588end
589put tra 3 r
590n = dv_cat t 0 view part ba 12288 r
591r = dv_join part ba 16384 4096 view part t 0 n trwork
592if eq r dvnone
593ret neg long 1
594end
595put tra 4 r
596ret tr_head ba bj
597end
598
599note tr_tree ba bj: rmtree(ROOT) (errors ignored), makedirs ROOT/dirname(path) and WORK, and the
600note broken source written to ROOT/path; 0 or a negative error.
601fn tr_tree ba view uchar 32768 bj view uchar 16384 to long
602t = new uchar 4096 0
603r = dv_rmrf part ba 12288 get tra 3
604dn = dv_dirn part bj 1024 get trb 1
605n = dv_join t part ba 12288 get tra 3 part bj 1024 dn
606if eq n dvnone
607ret neg long 36
608end
609r = dv_mkdirp view part t 0 n
610if lt r 0
611ret r
612end
613r = dv_mkdirp part ba 16384 get tra 4
614if lt r 0
615ret r
616end
617if eq get trb 5 0
618ret 0
619end
620n = dv_join t part ba 12288 get tra 3 part bj 1024 get trb 1
621if eq n dvnone
622ret neg long 36
623end
624ret dv_spit view part t 0 n part bj 6144 get trb 3
625end
626
627note ======== one tool action
628
629note tr_tcode bj: the tool of the current action: 1 read_file, 2 omega_check, 3 anything else.
630fn tr_tcode bj view uchar 16384 to ulong
631if dv_eq part bj 5120 get trb 4 trrf
632ret 1
633end
634if dv_eq part bj 5120 get trb 4 troc
635ret 2
636end
637ret 3
638end
639
640note tr_argc ba bj p: the check arguments WORK OMC KIND K N A appended to the packed list in bj.
641fn tr_argc ba view uchar 32768 bj edit uchar 16384 p ulong to ulong
642nb = new uchar 32 0
643if eq get trv 1 0
644ret dvnone
645end
646q = dv_pkadd part bj 12288 4096 p part ba 16384 get tra 4
647q = dv_pkadd part bj 12288 4096 q part ba 4096 get tra 1
648for i ulong 4
649m = dv_fmtl nb 0 get trv add i 1
650q = dv_pkadd part bj 12288 4096 q view part nb 0 m
651end
652ret q
653end
654
655note tr_argr bj p ib it: the replace arguments OLD NEW (strings of the parsed answer) appended.
656fn tr_argr bj edit uchar 16384 p ulong ib view uchar 8192 it view uint dvtcap to ulong
657ov = new uchar 4096 0
658v = dv_jkey ib it 0 trkold
659n = dv_jstr ib it v ov
660if lt n 0
661ret dvnone
662end
663q = dv_pkadd part bj 12288 4096 p view part ov 0 ulong n
664v = dv_jkey ib it 0 trknew
665n = dv_jstr ib it v ov
666if lt n 0
667ret dvnone
668end
669ret dv_pkadd part bj 12288 4096 q view part ov 0 ulong n
670end
671
672note tr_pkpa bj p pa n: pa[0, n) appended to the packed list in bj[12288, ..); as dv_pkadd.
673fn tr_pkpa bj edit uchar 16384 p ulong pa view uchar 4096 n ulong to ulong
674ret dv_pkadd part bj 12288 4096 p part pa 0 n
675end
676
677note tr_argv ba bj ib it: the TOOL command line of the parsed answer packed into bj[12288, ..);
678note its length or dvnone.
679fn tr_argv ba view uchar 32768 bj edit uchar 16384 ib view uchar 8192 it view uint dvtcap to ulong
680pa = new uchar 4096 0
681c = tr_tcode bj
682p = dv_pkinit part bj 12288 4096
683p = dv_pkadd part bj 12288 4096 p part ba 0 get tra 0
684if eq c 1
685p = dv_pkadd part bj 12288 4096 p trcread
686end
687if eq c 2
688p = dv_pkadd part bj 12288 4096 p trcchk
689end
690if eq c 3
691p = dv_pkadd part bj 12288 4096 p trcrepl
692end
693p = dv_pkadd part bj 12288 4096 p part ba 12288 get tra 3
694n = dv_jstr ib it dv_jkey ib it 0 trkpath pa
695if lt n 0
696ret dvnone
697end
698p = tr_pkpa bj p pa ulong n
699if eq c 2
700ret tr_argc ba bj p
701end
702if eq c 3
703ret tr_argr bj p ib it
704end
705ret p
706end
707
708note tr_split ob e fs: the ASCII-whitespace-separated fields of ob[0, e): the first four as
709note (start, length) pairs in fs; the field count.
710fn tr_split ob view uchar 16384 e ulong fs edit ulong 8 to ulong
711k = ulong 0
712s = ulong 0
713inw = false
714i = ulong 0
715while lt i e bound 16385
716if tr_aws get ob i
717if inw
718if lt k 4
719put fs mul k 2 s
720put fs add mul k 2 1 sub i s
721end
722k = add k 1
723inw = false
724end
725else
726if not inw
727s = i
728inw = true
729end
730end
731i = add i 1
732end
733if inw
734if lt k 4
735put fs mul k 2 s
736put fs add mul k 2 1 sub e s
737end
738k = add k 1
739end
740ret k
741end
742
743note tr_parse ob n: whether ob[0, n) is "OMNT1 st ol cl\n" then ol + cl body bytes; sets trs.
744fn tr_parse ob view uchar 16384 n ulong to bool
745fs = new ulong 8 0
746nl = dv_find part ob 0 n dvnl 0
747if eq nl dvnone
748ret false
749end
750if lt tr_split ob nl fs 4
751ret false
752end
753if not dv_eq part ob get fs 0 get fs 1 trmagic
754ret false
755end
756st = dv_atou part ob get fs 2 get fs 3
757ol = dv_atou part ob get fs 4 get fs 5
758cl = dv_atou part ob get fs 6 get fs 7
759if or or eq st dvnone eq ol dvnone eq cl dvnone
760ret false
761end
762bl = sub sub n nl 1
763if gt ol bl
764ret false
765end
766if ne cl sub bl ol
767ret false
768end
769put trs 0 st
770put trs 1 add nl 1
771put trs 2 ol
772put trs 3 add add nl 1 ol
773put trs 4 cl
774if eq st 1
775put trs 4 0
776end
777ret true
778end
779
780note tr_none ob w: observation for an action without a record into ob and trs (status None):
781note w 0..255 "exit w", 256 + s "exit -s", -110 "timeout", -1000 "malformed record", else
782note "spawn error".
783fn tr_none ob edit uchar 16384 w long to void
784n = ulong 0
785if ge w 0
786n = dv_cat ob 0 trexit
787if gt w 255
788n = dv_byte ob n 45
789n = dv_fmtl ob n sub w 256
790else
791n = dv_fmtl ob n w
792end
793else
794if eq w -110
795n = dv_cat ob 0 trtmo
796else
797if eq w -1000
798n = dv_cat ob 0 trbadr
799else
800n = dv_cat ob 0 trspawn
801end
802end
803end
804put trs 0 dvnone
805put trs 1 0
806put trs 2 n
807put trs 3 0
808put trs 4 0
809end
810
811note tr_got gb ba src tape: the head and the next turn's state and a newline into gb; end or dvnone.
812fn tr_got gb edit uchar 16384 ba view uchar 32768 src view uchar * tape view uint dvtcap to ulong
813sv = dv_jkey src tape get trs 5 trkstat
814p = dv_cat gb 0 part ba 24576 get tra 6
815n = dv_jstr src tape sv part gb p sub 16384 p
816if lt n 0
817ret dvnone
818end
819ret dv_byte gb add p ulong n 10
820end
821
822note tr_tail gb p ob: [Source:\n CONTENT][Tool result: OBS\n]Answer:\n appended at gb[p].
823fn tr_tail gb edit uchar 16384 p ulong ob view uchar 16384 to ulong
824q = p
825if gt get trs 4 0
826q = dv_cat gb q trsrc
827q = dv_cat gb q part ob get trs 3 get trs 4
828end
829if gt get trs 2 0
830q = dv_cat gb q trres
831q = dv_cat gb q part ob get trs 1 get trs 2
832q = dv_byte gb q 10
833end
834ret dv_cat gb q trans
835end
836
837note tr_miss bj ob: print "MISMATCH id fault tool status ST REPR" (REPR: repr(obs)[:200]).
838fn tr_miss bj view uchar 16384 ob view uchar 16384 to void
839o = new uchar 8192 0
840p = dv_cat o 0 trmis
841p = dv_cat o p part bj 0 get trb 0
842p = dv_byte o p 32
843p = dv_cat o p part bj 4096 get trb 2
844p = dv_byte o p 32
845p = dv_cat o p part bj 5120 get trb 4
846p = dv_cat o p trstw
847if eq get trs 0 dvnone
848p = dv_cat o p trnone
849else
850p = dv_fmtu o p get trs 0
851end
852p = dv_byte o p 32
853p = tr_repr o p part ob get trs 1 get trs 2 200
854p = dv_byte o p 10
855if ne p dvnone
856x = dv_out view part o 0 p
857end
858end
859
860note tr_wline wb wn: print " want REPR" (REPR: repr of the last 200 code points of wb[0, wn)).
861fn tr_wline wb view uchar 16384 wn ulong to void
862o = new uchar 2048 0
863s = tr_back wb wn 200
864p = dv_cat o 0 trwant
865p = tr_repr o p part wb s sub wn s 1000
866p = dv_byte o p 10
867if ne p dvnone
868x = dv_out view part o 0 p
869end
870end
871
872note tr_bad bj m: print "ERROR id m"; 1 (a failed run).
873fn tr_bad bj view uchar 16384 m view uchar * to long
874o = new uchar 4096 0
875p = dv_cat o 0 trerr
876p = dv_cat o p part bj 0 get trb 0
877p = dv_byte o p 32
878p = dv_cat o p m
879p = dv_byte o p 10
880if ne p dvnone
881x = dv_out view part o 0 p
882end
883ret 1
884end
885
886note tr_same gb p wb wn: whether gb[0, p) (p not dvnone) equals wb[0, wn).
887fn tr_same gb view uchar 16384 p ulong wb view uchar 16384 wn ulong to bool
888if eq p dvnone
889ret false
890end
891ret dv_eq part gb 0 p part wb 0 wn
892end
893
894note tr_judge ob src tape ba bj: rebuild the prompt from the observation in ob and trs and compare
895note it with the next turn's prompt; 0 equal, 1 different (MISMATCH printed) or a fixture error.
896fn tr_judge ob view uchar 16384 src view uchar * tape view uint dvtcap ba view uchar 32768 bj view uchar 16384 to long
897gb = new uchar 16384 0
898wb = new uchar 16384 0
899p = tr_got gb ba src tape
900p = tr_tail gb p ob
901wn = dv_jstr src tape dv_jkey src tape get trs 5 trkprom wb
902if lt wn 0
903ret tr_bad bj trewant
904end
905if tr_same gb p wb ulong wn
906ret 0
907end
908tr_miss bj ob
909tr_wline wb ulong wn
910ret 1
911end
912
913note tr_act ba bj ob: run the packed TOOL command (stdout to the capture file, stderr to /dev/null,
914note 120 s deadline) and leave its observation in ob and trs.
915fn tr_act ba view uchar 32768 bj view uchar 16384 ob edit uchar 16384 to void
916pl = get trs 6
917cl = get tra 5
918pid = dv_spawn part bj 12288 pl part ba 20480 cl trnull
919w = neg long 1
920if ge pid 0
921w = dv_wait pid 120000000000
922end
923if ne w 0
924tr_none ob w
925ret
926end
927n = dv_slurp part ba 20480 cl ob
928if lt n 0
929tr_none ob -1000
930ret
931end
932if not tr_parse ob ulong n
933tr_none ob -1000
934end
935end
936
937note tr_exec src tape ba bj: run the packed TOOL command, read its record, judge; as tr_judge.
938fn tr_exec src view uchar * tape view uint dvtcap ba view uchar 32768 bj view uchar 16384 to long
939ob = new uchar 16384 0
940tr_act ba bj ob
941put trn 3 add get trn 3 1
942ret tr_judge ob src tape ba bj
943end
944
945note tr_raw src tape t ib: turn t's raw answer cut and stripped into ib; its length, or -1.
946fn tr_raw src view uchar * tape view uint dvtcap t ulong ib edit uchar 8192 to long
947rb = new uchar 8192 0
948n = dv_jstr src tape dv_jkey src tape t trkraw rb
949if lt n 0
950ret n
951end
952ret long tr_text rb ulong n ib
953end
954
955note tr_ans ib it m ba bj: parse the answer ib[0, m), store its tool name and pack its TOOL
956note command line into bj; 0, or 1 after an ERROR line.
957fn tr_ans ib view uchar 8192 it edit uint dvtcap m ulong ba view uchar 32768 bj edit uchar 16384 to long
958r = js_init it jsmaxn 64
959if lt r 0
960ret tr_bad bj trejson
961end
962r = js_parse ib it m
963if lt r 0
964ret tr_bad bj trejson
965end
966k = dv_jstr ib it dv_jkey ib it 0 trktool part bj 5120 1024
967if lt k 0
968ret tr_bad bj tretool
969end
970put trb 4 ulong k
971p = tr_argv ba bj ib it
972if eq p dvnone
973ret tr_bad bj treargv
974end
975put trs 6 p
976ret 0
977end
978
979note tr_turn src tape t ba bj: the action of turn t judged against the following turn; as tr_judge.
980fn tr_turn src view uchar * tape view uint dvtcap t ulong ba view uchar 32768 bj edit uchar 16384 to long
981ib = new uchar 8192 0
982it = new uint dvtcap 0
983put trs 5 js_enext tape t
984m = tr_raw src tape t ib
985if lt m 0
986ret tr_bad bj treraw
987end
988if ne tr_ans ib it ulong m ba bj 0
989ret 1
990end
991ret tr_exec src tape ba bj
992end
993
994note tr_run src tape r ba bj: replay run r; 0 passed, 1 failed.
995fn tr_run src view uchar * tape view uint dvtcap r ulong ba edit uchar 32768 bj edit uchar 16384 to long
996for i ulong 8
997put trb i 0
998end
999j = dv_jkey src tape r trkjob
1000if lt tr_job src tape j bj 0
1001ret tr_bad bj trejob
1002end
1003if lt tr_paths ba bj 0
1004ret tr_bad bj trepath
1005end
1006if lt tr_tree ba bj 0
1007ret tr_bad bj tretree
1008end
1009tv = dv_jkey src tape r trkturn
1010if ne js_type tape tv jsarr
1011ret tr_bad bj treturn
1012end
1013t = js_first tape tv
1014while ne t jsnone bound 16777216
1015nx = js_enext tape t
1016if eq nx jsnone
1017ret 0
1018end
1019if ne tr_turn src tape t ba bj 0
1020ret 1
1021end
1022t = nx
1023end
1024ret 0
1025end
1026
1027note tr_args ba: TOOL, OMC, OUTDIR, ROUND40/final-selected.json and OUTDIR/.tool-replay.out into ba.
1028fn tr_args ba edit uchar 32768 to long
1029t = new uchar 4096 0
1030n = dv_arg 1 part ba 0 4095
1031if lt n 0
1032ret n
1033end
1034put tra 0 ulong n
1035n = dv_arg 2 part ba 4096 4095
1036if lt n 0
1037ret n
1038end
1039put tra 1 ulong n
1040n = dv_arg 4 part ba 8192 4095
1041if lt n 0
1042ret n
1043end
1044put tra 2 ulong n
1045n = dv_arg 3 part ba 28672 4095
1046if lt n 0
1047ret n
1048end
1049q = dv_cat part ba 28672 4095 ulong n trsel
1050if eq q dvnone
1051ret neg long 36
1052end
1053put tra 7 q
1054q = dv_cat t 0 view part ba 8192 get tra 2
1055q = dv_join part ba 20480 4095 view part t 0 q trcap
1056if eq q dvnone
1057ret neg long 36
1058end
1059put tra 5 q
1060ret 0
1061end
1062
1063note tr_sum: print "TOOL-REPLAY runs=N pass=P fail=F actions=A".
1064fn tr_sum to void
1065o = new uchar 256 0
1066p = dv_cat o 0 trsum1
1067p = dv_fmtu o p get trn 0
1068p = dv_cat o p trsum2
1069p = dv_fmtu o p get trn 1
1070p = dv_cat o p trsum3
1071p = dv_fmtu o p get trn 2
1072p = dv_cat o p trsum4
1073p = dv_fmtu o p get trn 3
1074p = dv_byte o p 10
1075x = dv_out view part o 0 p
1076end
1077
1078fn main to int
1079src = new uchar 983040 0
1080tape = new uint dvtcap 0
1081ba = new uchar 32768 0
1082bj = new uchar 16384 0
1083if lt dv_argc 5
1084x = dv_err trusage
1085ret 2
1086end
1087if lt tr_args ba 0
1088x = dv_err trusage
1089ret 2
1090end
1091n = dv_jload view part ba 28672 get tra 7 src tape
1092if lt n 0
1093x = dv_err treload
1094ret 2
1095end
1096if ne js_type tape 0 jsarr
1097x = dv_err treload
1098ret 2
1099end
1100r = js_first tape 0
1101while ne r jsnone bound 16777216
1102f = tr_run src tape r ba bj
1103put trn 0 add get trn 0 1
1104if eq f 0
1105put trn 1 add get trn 1 1
1106else
1107put trn 2 add get trn 2 1
1108end
1109r = js_enext tape r
1110end
1111u = unlink ba 20480 get tra 5
1112tr_sum
1113if eq get trn 2 0
1114ret 0
1115end
1116ret 1
1117end