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 / sovereign-core / om / hive-task / drive.om

3209 lines · 59242 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/hive-task/drive.om: the checks of the hive forge task rules (issue #925, DRA-288, DRA-162).
3note Build with the pinned omc: omc < om/hive-task/drive.om > drive
4note Run: drive MODE RULES COCKPIT SCRATCH DIR OMC MAIN
5note MODE cases, http, table or migrate
6note RULES om/hive-task/task.om compiled by the pinned omc
7note COCKPIT the spark-cockpit-rs binary under test (the branch build, or main's as the control)
8note SCRATCH an existing empty directory; every database, log and file of the run is made there
9note DIR om/hive-task (cases.tsv and task.om are read from it)
10note OMC the pinned omc; mode table rebuilds mutants of task.om with it
11note MAIN spark-cockpit-rs built from main; mode migrate opens main's database with COCKPIT
12note Modes:
13note cases runs RULES once per line of cases.tsv and compares stdout and exit status.
14note http starts COCKPIT on an ephemeral loopback port with a scratch database and drives
15note /api/hive/forge/* over HTTP: T1 to T3, stale attempts, expired leases, generation
16note exhaustion, ttl range (400 invalid), bad JSON bodies (400 invalid), artifact
17note binding, a held write lock, 400 claim races between two cockpit processes sharing
18note one database file, command-id replay (same body, no second change, 409
19note command_conflict on a different payload, across both processes and under a race),
20note a write lock held for 2 s, past a 1 s ttl, while a claim, heartbeat or submit waits
21note (the lease must be judged by the clock after the lock is granted), and a commit
22note that cannot complete (the cockpit must not answer 200 for it).
23note table seeds every combination of state, generation, fence, artifact, verdict, lease and
24note ttl for each operation, applies it through the API, and checks that RULES predicts
25note each outcome and next state; op 7 does the same for command ids (prior record,
26note same or different payload, id length and characters, task state); then shows
27note that mutants of task.om fail the checks.
28note migrate makes a database with MAIN, opens it with COCKPIT and checks the migration.
29note Output: one line per check, "PASS name ..." or "FAIL name ...", then a SUMMARY line. Exit 0
30note only when nothing failed.
31note Host programs on this test path only, never shipped: /usr/bin/sqlite3 (3.45.1) seeds rows the
32note API cannot create (any status, a generation near 2^53, an expired lease), holds a write lock
33note for the busy case and a read lock on a rollback-journal database for the commit case.
34note /usr/bin/env is a temporary bridge in cockexec, because execprog starts a program with an
35note empty environment.
36om2
37const NONE ulong 18446744073709551615
38const GMAX ulong 9007199254740991
39const ROUT ulong 0
40const RREQ ulong 1
41const RREQ2 ulong 2
42const RBODY ulong 3
43const RPATH ulong 4
44const REXEC ulong 5
45const RSQL ulong 6
46const RRESP ulong 7
47const RFILE ulong 8
48const RARG ulong 9
49const RTIN ulong 10
50const RTOBS ulong 11
51const RATT ulong 12
52const RSRC ulong 13
53const RCASE ulong 14
54const RDB ulong 15
55const RMISC ulong 16
56const RTOK ulong 17
57const RTID ulong 18
58const RAID ulong 19
59const RART ulong 20
60const RMUT ulong 21
61const RCMD ulong 22
62const RSAVE ulong 23
63const RROW ulong 24
64note g slots
65const GTOP ulong 0
66const GNREC ulong 1
67const GPASS ulong 2
68const GFAIL ulong 3
69const GNRUN ulong 4
70const GPORTA ulong 5
71const GPIDA ulong 7
72const GBODY ulong 9
73const GLAST ulong 10
74const GT0 ulong 11
75const GROW ulong 12
76const GINC ulong 13
77const GTCH ulong 14
78const GP1 ulong 15
79const GP2 ulong 17
80const GP3 ulong 19
81const GAL0 ulong 22
82const GRZ ulong 30
83const GLREF ulong 33
84const GLBUSY ulong 34
85const GLOTH ulong 35
86const GRECL ulong 36
87const GRALW ulong 37
88const GCMD ulong 38
89const GP4 ulong 40
90const GQ ulong 42
91const GH ulong 45
92const GK0 ulong 48
93global m uchar 12582912
94global g ulong 64
95global al ulong 8
96global cu ulong 32
97global bs ulong 32
98global lm ulong 32
99global tc ulong 8
100global tv ulong 8
101global tr ulong 512
102global tk ulong 256
103text dq "\""
104text tusage "usage: drive MODE RULES COCKPIT SCRATCH DIR OMC MAIN\n"
105text tderr "DRIVER ERROR "
106text tpass "PASS "
107text tfail "FAIL "
108text thttp_ " http="
109text mcases "cases"
110text mhttp "http"
111text mtable "table"
112text mmig "migrate"
113text tsumm "SUMMARY mode="
114text tsp " pass="
115text tsf " fail="
116text tsn " not_run="
117text tsi " inconclusive="
118text fcases "/cases.tsv"
119text fsrc "/task.om"
120text frin "/rules.in"
121text frout "/rules.out"
122text fmuts "/mutant.om"
123text fmutb "/mutant.bin"
124text fsql "/q.sql"
125text fsqo "/q.out"
126text flog "/cockpit.log"
127text dhome "/home"
128text ftin "/table.in"
129text ftobs "/table.obs"
130text fhttpdb "/http.db"
131text fmigdb "/migrate.db"
132text fcomdb "/commit.db"
133text ftbl "/table-"
134text fdbs ".db"
135text envbin "/usr/bin/env"
136text ehome "HOME="
137text edb "HIVE_DB_PATH="
138text etok "AIEN_COCKPIT_TOKEN="
139text eaddr "AIEN_COCKPIT_ADDR=127.0.0.1:"
140text sqlbin "/usr/bin/sqlite3"
141text sqbatch "-batch"
142text sqbail "-bail"
143text sqhd ".timeout 10000\n"
144text sqbeg "BEGIN;\n"
145text sqcom "COMMIT;\n"
146text sqi1 "INSERT INTO forge_tasks (id, comb_id, project, module, ring, q, r, title, status, created_at) VALUES ('"
147text sqi2 "', 'c', 'scn', 'm', 0, 0, 0, 't', 'open', '2026-11-07T00:00:00Z');\n"
148text tins1 "INSERT INTO forge_tasks (id, comb_id, project, module, ring, q, r, title, status, claimed_by, created_at, attempt_id, generation, lease_expires_ms, artifact) VALUES ('x"
149text tins2 "', 'c', 'tbl', 'm', 0, 0, 0, 't', '"
150text tins3 "', 'agent-t', '2026-11-07T00:00:00Z', 'att-"
151text tins4 "', "
152text tins5 ", 'art-"
153text tins6 "');\n"
154text tnull "NULL"
155text tcomma ", "
156text tdump_ "SELECT id, status, generation FROM forge_tasks WHERE id GLOB 'x*' ORDER BY CAST(substr(id, 2) AS INTEGER);\n"
157text sopen "open"
158text sclaim "claimed"
159text ssub "submitted"
160text sver "verified"
161text sfail "failed"
162text sunk "unknown"
163text tpost "POST "
164text tget "GET "
165text thttp " HTTP/1.1"
166text hhost "Host: 127.0.0.1"
167text hauth "Authorization: Bearer "
168text hclose "Connection: close"
169text hctype "Content-Type: application/json"
170text hclen "Content-Length: "
171text pclaim "/api/hive/forge/claim"
172text phb "/api/hive/forge/heartbeat"
173text psub "/api/hive/forge/submit"
174text pver "/api/hive/forge/verify"
175text prec "/api/hive/forge/reclaim"
176text ptasks "/api/hive/forge/tasks"
177text jtid "{\"task_id\":\""
178text jagent ",\"agent_id\":\""
179text jaid ",\"attempt_id\":\""
180text jgenk ",\"generation\":"
181text jttl ",\"ttl_secs\":"
182text jbr ",\"branch\":\"b-core925\",\"artifact\":\""
183text jart ",\"artifact\":\""
184text jvby ",\"verifier\":\""
185text tver "v-core925"
186text jvd ",\"verdict\":"
187text jtrue "true"
188text jfalse "false"
189text jnotes ",\"notes\":\"n-core925\"}"
190text cbr "}"
191text jcmd ",\"command_id\":\""
192text jrcmd "{\"command_id\":\""
193text jrend "\"}"
194text jbadm "{\"task_id\":\"s9\",\"ttl_secs\":300}"
195text jbadj "{\"task_id\":"
196text jcreate "{\"project\":\"scn\",\"module\":\"m\",\"ring\":0,\"title\":\"t\",\"description\":\"d\",\"parent_comb_id\":null}"
197text kaid "\"attempt_id\":\""
198text kgen "\"generation\":"
199text kstat "\"status\":"
200text kcode "\"code\":"
201text kretry "\"retryable\":"
202text kartk "\"artifact\":"
203text kbr "\"branch\":"
204text kvby "\"verified_by\":"
205text kvn "\"verify_notes\":"
206text krecl "\"reclaimed_count\":"
207text vopen "\"open\""
208text vclaim "\"claimed\""
209text vsub "\"submitted\""
210text vver "\"verified\""
211text vbusy "\"busy\""
212text vrefus "\"refused\""
213text vcmdc "\"command_conflict\""
214text vinval "\"invalid\""
215text vsha2 "\"sha256:s2\""
216text vsha3 "\"sha256:s3\""
217text vbr "\"b-core925\""
218text vver2 "\"v-core925\""
219text vnotes "\"n-core925\""
220text agw2 "w2"
221text agw3 "w3"
222text agwk "worker"
223text agw5 "w5"
224text agw6 "w6"
225text agw9 "w9"
226text agra "ra"
227text agrb "rb"
228text agold "old"
229text agnew "new"
230text agentc "agent-c"
231text agentt "agent-t"
232text ts1 "s1"
233text ts2 "s2"
234text ts3 "s3"
235text ts4 "s4"
236text ts5 "s5"
237text ts6 "s6"
238text ts7 "s7"
239text ts8 "s8"
240text ts9 "s9"
241text ts10 "s10"
242text ts11 "s11"
243text ts12 "s12"
244text ts13 "s13"
245text ts14 "s14"
246text ts15 "s15"
247text ts16 "s16"
248text ts17 "s17"
249text ts18 "s18"
250text ts19 "s19"
251text tc1 "c1"
252text cidc1 "core925-c1"
253text cidh1 "core925-h1"
254text cidu1 "core925-u1"
255text cidv1 "core925-v1"
256text cidr1 "core925-r1"
257text cidx1 "core925-x1"
258text cidb1 "core925-b1"
259text cidk1 "core925-k1"
260text cidbad "core925/1"
261text cidq "core925-q"
262text cidp "core925-p"
263text cidl1 "core925-l1"
264text cidl2 "core925-l2"
265text tslx "/x"
266text tm1 "m1"
267text tm2 "m2"
268text tm3 "m3"
269text asha0 "sha256:none"
270text asha2 "sha256:s2"
271text asha3 "sha256:s3"
272text ashax "sha256:other"
273text tattp "att-"
274text tattx "att-x"
275text tartp "art-"
276text tartx "art-x"
277text tartn "art-new"
278text ttokp "core925-"
279text n_create "create_task_201"
280text n_t1 "T1_verify_open_refused"
281text n_t2a "T2_setup_claim"
282text n_t2b "T2_setup_submit"
283text n_t2c "T2_setup_verify"
284text n_t2 "T2_claim_verified_refused"
285text n_t3 "T3_submit_verified_refused"
286text n_va "verify_setup_claim"
287text n_vb "submit_persists_branch_and_artifact"
288text n_vc "verify_wrong_artifact_refused"
289text n_vd "verify_wrong_generation_refused"
290text n_ve "verify_without_verifier_rejected_400"
291text n_vf "verify_persists_verifier_and_notes"
292text n_sa "stale_setup_first_claim"
293text n_sb "stale_setup_same_name_reclaim_generation_plus_one"
294text n_sc "stale_attempt_heartbeat_refused"
295text n_sd "stale_attempt_submit_refused"
296text n_se "current_attempt_heartbeat_accepted"
297text n_ea "expired_setup_claim"
298text n_eb "expired_heartbeat_refused_unchanged"
299text n_ec "expired_submit_refused_unchanged"
300text n_xa "exhaust_setup_seed_generations"
301text n_xb "claim_at_max_generation_refused"
302text n_xc "claim_reaches_max_generation_exactly"
303text n_xd "heartbeat_at_max_generation_accepted"
304text n_ta "claim_ttl_zero_invalid_400"
305text n_tb "claim_ttl_over_one_day_invalid_400"
306text n_tc "claim_ttl_one_day_accepted"
307text n_td "heartbeat_ttl_zero_invalid_400"
308text n_ja "json_missing_field_400_invalid"
309text n_jb "json_malformed_400_invalid"
310text n_ba "busy_setup_sqlite3_holds_write_lock"
311text n_bb "busy_claim_refused_503_retryable"
312text n_bc "busy_retry_same_command_id_granted"
313text n_ra "replay_setup_claim_with_command_id"
314text n_rb "replay_identical_claim_same_body"
315text n_rc "replay_identical_claim_row_unchanged"
316text n_rd "replay_different_payload_409_command_conflict"
317text n_re "replay_heartbeat_identical_no_lease_extension"
318text n_rf "replay_submit_identical_same_body"
319text n_rg "replay_verify_identical_same_body"
320text n_rh "replay_reclaim_identical_no_second_sweep"
321text n_ri "command_id_too_long_400"
322text n_rj "command_id_128_bytes_valid"
323text n_rk "command_id_bad_char_400"
324text n_rl "replay_after_refusal_decided_again"
325text n_rm "command_record_written_once"
326text n_rx "replay_across_two_cockpits_same_body"
327text n_ry "replay_across_two_cockpits_conflict_409"
328text n_rr "replay_race_two_cockpits_one_record"
329text n_cf "commit_unsafe_db_refused_at_start_row_unchanged"
330text n_cfa "commit_setup_rollback_journal_and_reader"
331text n_race "race_two_cockpits_single_grant"
332text n_lk1 "lock_setup_hold_and_send"
333text n_lk2 "lock_claim_ttl_crossed_lease_live_at_grant"
334text n_lk3 "lock_claim_cmd_ttl_crossed_lease_live_at_grant"
335text n_lk4 "lock_heartbeat_expired_while_waiting_refused_row_unchanged"
336text n_lk5 "lock_heartbeat_cmd_expired_while_waiting_refused_not_recorded"
337text n_lk6 "lock_submit_expired_while_waiting_refused_row_unchanged"
338text n_ma "migrate_setup_main_claim"
339text n_mb "migrate_setup_main_submit"
340text n_mc "migrate_old_schema_13_columns"
341text n_md "migrate_new_schema_21_columns"
342text n_me "migrate_rows_backfilled"
343text n_mf "migrate_backfilled_lease_held"
344text n_mg "migrate_open_claim_generation_1"
345text n_mh "migrate_legacy_submitted_reworkable"
346text n_mi "migrate_command_table_present"
347text n_mj "migrate_reopen_idempotent"
348text n_tab "table_crosscheck"
349text n_trec "table_reap_count_matches"
350text tbusyw " waited_ms="
351text tbsql ".timeout 0\nBEGIN IMMEDIATE;\nSELECT 'held';\n"
352text tbheld "held"
353text tbrb "ROLLBACK;\n"
354text tbsql2 ".timeout 0\nBEGIN;\nSELECT 'held' FROM sqlite_master LIMIT 1;\n"
355text tdelj "PRAGMA journal_mode = DELETE;\n"
356text tdelw "delete\n"
357text tcomq "SELECT status || ' ' || generation FROM forge_tasks WHERE id = 'c1';\n"
358text tcopen "open 0\n"
359text tcom1 " served="
360text tcom2 " claim="
361text trow10 "SELECT generation || '|' || attempt_id || '|' || COALESCE(lease_expires_ms, 0) || '|' || status FROM forge_tasks WHERE id = 's10';\n"
362text texp1 "UPDATE forge_tasks SET status = 'claimed', claimed_by = 'w', attempt_id = 'a', generation = 1, lease_expires_ms = 1 WHERE id = '"
363text tqend "';\n"
364text tss13 "SELECT status FROM forge_tasks WHERE id = 's13';\n"
365text tclnl "claimed\n"
366text tcnt "SELECT (SELECT count(*) FROM forge_commands WHERE command_id IN ('core925-c1', 'core925-x1', 'core925-b1')) || ' ' || (SELECT count(*) FROM forge_commands WHERE command_id = 'core925/1' OR length(command_id) > 128);\n"
367text tcntw "3 0\n"
368text trq2 "SELECT (SELECT count(*) FROM forge_tasks WHERE id GLOB 'q[0-9]*' AND status = 'claimed' AND generation = 1) || ' ' || (SELECT count(*) FROM forge_commands WHERE command_id GLOB 'core925-q[0-9]*');\n"
369text tlive "SELECT CASE WHEN status = 'claimed' AND lease_expires_ms > CAST((julianday('now') - 2440587.5) * 86400000 AS INTEGER) THEN 'live' ELSE 'dead' END FROM forge_tasks WHERE id = '"
370text tlivew "live\n"
371text trowa "SELECT generation || '|' || attempt_id || '|' || COALESCE(lease_expires_ms, 0) || '|' || status FROM forge_tasks WHERE id = '"
372text tlkcnt "SELECT count(*) FROM forge_commands WHERE command_id = 'core925-l2';\n"
373text tlkcntw "0\n"
374text t4040 "40 40\n"
375text trr1 " trials=40 same_body="
376text trr2 " one_busy="
377text trr3 " other="
378text tcmdt "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'forge_commands';\n"
379text t1nl "1\n"
380text tcmdg "CREATE TABLE IF NOT EXISTS forge_commands (command_id TEXT PRIMARY KEY);\n"
381text tkupd1 "UPDATE forge_tasks SET status = 'verified', generation = 1 WHERE id = 'k"
382text td1 "SELECT COALESCE((SELECT status FROM forge_tasks WHERE id = 'k"
383text td2 "'), 'none') || '|' || COALESCE((SELECT generation FROM forge_tasks WHERE id = 'k"
384text td3 "'), 0) || '|' || (SELECT count(*) FROM forge_commands WHERE command_id = '"
385text td4 "');\n"
386text tz3 " 0 0 0 "
387text tsx6 "UPDATE forge_tasks SET generation = 9007199254740991 WHERE id = 's6';\nUPDATE forge_tasks SET generation = 9007199254740990 WHERE id = 's7';\n"
388text tss5 "SELECT status FROM forge_tasks WHERE id = 's5';\n"
389text tsubnl "submitted\n"
390text trq "SELECT count(*) FROM forge_tasks WHERE id GLOB 'r[0-9]*' AND status = 'claimed';\n"
391text tcols "SELECT count(*) FROM pragma_table_info('forge_tasks');\n"
392text t13 "13\n"
393text t21 "21\n"
394text tmrows "SELECT id || ' ' || status || ' ' || generation || ' ' || COALESCE(lease_expires_ms > 0, 0) || ' ' || COALESCE(verified_by, '-') FROM forge_tasks WHERE id GLOB 'm*' ORDER BY id;\n"
395text tmwant "m1 open 0 0 -\nm2 claimed 0 1 -\nm3 failed 0 0 migration\n"
396text trace1 " trials=400 one_grant="
397text trace2 " zero_grant="
398text trace3 " double_grant="
399text trace4 " loser_refused_409="
400text trace5 " loser_busy_503="
401text trace6 " loser_other="
402text trace7 " db_claimed="
403text ttab1 " rows="
404text ttab2 " mismatches="
405text ttab3 " chunks="
406text ttab4 " reclaimed_count="
407text ttab5 " rules_reaps="
408text tinc "INCONCLUSIVE table chunk "
409text tinc2 ": over 25 s after the cockpit started, so its 30 s reaper may have run\n"
410text tfrow "FAIL table row "
411text tfin " in="
412text tfrul " rules="
413text tfobs " observed="
414text tmut "mutant "
415text tmutk " killed: cases_failed="
416text tmuts " SURVIVED: cases_failed="
417text tmutt " table_mismatches="
418text tmuta "FAIL mutant anchor missing: "
419text tmutc "FAIL mutant refused by omc: "
420text tcase "case "
421text tcex " exit="
422text tcout " out="
423text tcsum "cases total="
424note mutants of task.om: the exact line, its replacement, and which occurrence
425text mf01 "el = or eq st OPEN eq st FAILED"
426text mt01 "el = true"
427text mf02 "ret HELD"
428text mt02 "ret ALLOW"
429text mf04 "if not live now exp"
430text mt04 "if lt now 0"
431text mf05 "ret and eq rgen gen eq am 1"
432text mt05 "ret eq rgen gen"
433text mt06 "ret eq am 1"
434text mf07 "if ge gen GMAX"
435text mt07 "if gt gen GMAX"
436text mf08 "if or ne rgen gen ne art 1"
437text mt08 "if ne rgen gen"
438text mt09 "if ne art 1"
439text mf10 "ret lt now exp"
440text mt10 "ret le now exp"
441text mf11 "ret and ge ttl 1 le ttl MAXTTL"
442text mt11 "ret ge ttl 1"
443text mf12 "ret add gen 1"
444text mt12 "ret gen"
445text mf13 "if eq vd 1"
446text mt13 "if le vd 1"
447text mf14 "if ne st SUBMTD"
448text mtfal "if false"
449text mf15 "if or eq code SQBUSY eq code SQLOCK"
450text mt15 "if eq code SQBUSY"
451text mf16 "if ne st CLAIMED"
452text mf17 "if eq dig 1"
453text mf18 "if eq prior 0"
454text mf19 "if eq sdec BADTTL"
455text mf20 "if ge sdec RETRY"
456text mf21 "if or eq idl 0 gt idl CMDMAX"
457text mt21 "if eq idl 0"
458text mf22 "if ne bad 0"
459text mf23 "if ne sdec ALLOW"
460text mf24 "ret prior"
461text mt24 "ret 0"
462text mf25 "if and eq dc ALLOW eq sdec ALLOW"
463text mt25 "if eq dc ALLOW"
464text mf26 "if ne has 1"
465text mf27 "if or eq dc REPLAY eq dc CONFLICT"
466
467note ---- arena regions: m holds every buffer; region r spans bs[r] up to lm[r], cursor cu[r].
468fn setreg r ulong sz ulong to void
469b = get g GTOP
470e = add b sz
471if gt e len m
472exit 70
473end
474put bs r b
475put cu r b
476put lm r e
477put g GTOP e
478end
479
480fn setup to void
481setreg ROUT 1048576
482setreg RREQ 131072
483setreg RREQ2 131072
484setreg RBODY 65536
485setreg RPATH 4096
486setreg REXEC 16384
487setreg RSQL 2097152
488setreg RRESP 1048576
489setreg RFILE 2097152
490setreg RARG 8192
491setreg RTIN 1048576
492setreg RTOBS 524288
493setreg RATT 4096
494setreg RSRC 262144
495setreg RCASE 65536
496setreg RDB 4096
497setreg RMISC 4096
498setreg RTOK 256
499setreg RTID 256
500setreg RAID 256
501setreg RART 256
502setreg RMUT 262144
503setreg RCMD 512
504setreg RSAVE 131072
505setreg RROW 4096
506end
507
508fn rsz r ulong to void
509put cu r get bs r
510end
511
512fn rln r ulong to ulong
513ret sub get cu r get bs r
514end
515
516fn pb r ulong c uchar to void
517p = get cu r
518if ge p get lm r
519exit 71
520end
521put m p c
522put cu r add p 1
523end
524
525fn ap r ulong t view uchar * to void
526for i ulong len t
527pb r get t i
528end
529end
530
531fn apm r ulong o ulong n ulong to void
532i = ulong 0
533while lt i n bound 999999
534pb r get m add o i
535i = add i 1
536end
537end
538
539fn apr r ulong s ulong to void
540apm r get bs s rln s
541end
542
543fn apa r ulong i ulong to void
544apm r add get bs RARG mul i 1024 get al i
545end
546
547fn apd r ulong v ulong to void
548p = ulong 1
549while le p div v 10 bound 20
550p = mul p 10
551end
552while ne p 0 bound 21
553pb r uchar add div v p 48
554v = rem v p
555p = div p 10
556end
557end
558
559fn apl r ulong v long to void
560if lt v 0
561pb r 45
562apd r ulong neg v
563else
564apd r ulong v
565end
566end
567
568fn hexd v uchar to uchar
569if lt v 10
570ret add v 48
571end
572ret add v 87
573end
574
575fn sp r ulong to void
576pb r 32
577end
578
579fn nlr r ulong to void
580pb r 10
581end
582
583note ---- output
584fn flush to void
585b = get bs ROUT
586n = sub get cu ROUT b
587o = ulong 0
588while lt o n bound 999999
589k = write 1 m add b o sub n o
590if le k 0
591exit 73
592end
593o = add o ulong k
594end
595put cu ROUT b
596end
597
598fn say t view uchar * to void
599ap ROUT t
600end
601
602fn nl to void
603pb ROUT 10
604flush
605end
606
607fn ck yes bool t view uchar * to void
608if not yes
609say tderr
610say t
611nl
612exit 80
613end
614end
615
616fn inc s ulong to void
617put g s add get g s 1
618end
619
620fn mark ok bool to void
621if ok
622say tpass
623inc GPASS
624else
625say tfail
626inc GFAIL
627end
628end
629
630fn rep ok bool name view uchar * to void
631mark ok
632say name
633say thttp_
634apd ROUT get g GLAST
635nl
636end
637
638note ---- arena scanning
639fn findc o ulong e ulong c uchar to ulong
640p = o
641while lt p e bound 999999
642if eq get m p c
643ret p
644end
645p = add p 1
646end
647ret e
648end
649
650fn pnum o ulong e ulong to ulong
651v = ulong 0
652d = ulong 0
653p = o
654while lt p e bound 64
655c = get m p
656if or lt c 48 gt c 57
657break
658end
659if eq d 20
660ret NONE
661end
662v = add mul v 10 ulong sub c 48
663p = add p 1
664d = add d 1
665end
666if eq d 0
667ret NONE
668end
669ret v
670end
671
672fn same a ulong b ulong n ulong to bool
673i = ulong 0
674while lt i n bound 999999
675if ne get m add a i get m add b i
676ret false
677end
678i = add i 1
679end
680ret true
681end
682
683fn mtext a ulong t view uchar * to bool
684for i ulong len t
685if ne get m add a i get t i
686ret false
687end
688end
689ret true
690end
691
692note rtext: region r holds exactly the bytes of t.
693fn rtext r ulong t view uchar * to bool
694if ne rln r len t
695ret false
696end
697ret mtext get bs r t
698end
699
700note nxt: the next decimal number at g[s] (positions up to g[s+1]), skipping other bytes.
701fn nxt s ulong to ulong
702p = get g s
703e = get g add s 1
704while lt p e bound 999999
705c = get m p
706if and ge c 48 le c 57
707break
708end
709p = add p 1
710end
711v = pnum p e
712q = p
713while lt q e bound 64
714c2 = get m q
715if or lt c2 48 gt c2 57
716break
717end
718q = add q 1
719end
720put g s q
721ret v
722end
723
724note ---- arguments
725fn args to void
726n = argcnt
727if ne n 8
728say tusage
729flush
730exit 2
731end
732for i ulong 8
733k = argread i m add get bs RARG mul i 1024 1023
734if le k 0
735say tusage
736flush
737exit 2
738end
739put al i ulong k
740end
741end
742
743fn argis i ulong t view uchar * to bool
744if ne get al i len t
745ret false
746end
747ret mtext add get bs RARG mul i 1024 t
748end
749
750note ---- files
751fn psc t view uchar * to void
752rsz RPATH
753apa RPATH 4
754ap RPATH t
755end
756
757fn pdir t view uchar * to void
758rsz RPATH
759apa RPATH 5
760ap RPATH t
761end
762
763note O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC = 524865; O_RDONLY|O_CLOEXEC = 524288;
764note O_WRONLY|O_CREAT|O_APPEND|O_CLOEXEC = 525377.
765fn fopw mode int to int
766fd = fopen m get bs RPATH rln RPATH 524865 mode
767ret int fd
768end
769
770fn fopr to int
771fd = fopen m get bs RPATH rln RPATH 524288 0
772ret int fd
773end
774
775fn fopa to int
776fd = fopen m get bs RPATH rln RPATH 525377 420
777ret int fd
778end
779
780fn wall fd int r ulong to bool
781b = get bs r
782n = rln r
783o = ulong 0
784while lt o n bound 999999
785k = write fd m add b o sub n o
786if le k 0
787ret false
788end
789o = add o ulong k
790end
791ret true
792end
793
794fn wfile r ulong to bool
795fd = fopw 420
796if lt fd 0
797ret false
798end
799ok = wall fd r
800c = fclose fd
801ret and ok eq c 0
802end
803
804fn rfile r ulong to long
805rsz r
806fd = fopr
807if lt fd 0
808ret long fd
809end
810p = get cu r
811e = get lm r
812while true bound 999999
813k = read fd m p sub e p
814if lt k 0
815c = fclose fd
816ret k
817end
818if eq k 0
819break
820end
821p = add p ulong k
822end
823c2 = fclose fd
824ck lt p e fcases
825put cu r p
826ret long sub p get bs r
827end
828
829note ---- processes: REXEC holds the packed argument list (u16 count, u16-length records).
830fn xbeg to void
831rsz REXEC
832pb REXEC 0
833pb REXEC 0
834put g GNREC 0
835end
836
837fn xhdr n ulong to void
838pb REXEC uchar and n 255
839pb REXEC uchar shr n 8
840inc GNREC
841end
842
843fn xa t view uchar * to void
844xhdr len t
845ap REXEC t
846end
847
848fn xreg r ulong to void
849xhdr rln r
850apr REXEC r
851end
852
853fn xarg i ulong to void
854xhdr get al i
855apa REXEC i
856end
857
858fn spawn fi int fo int fe int to int
859b = get bs REXEC
860n = get g GNREC
861put m b uchar and n 255
862put m add b 1 uchar shr n 8
863pid = fork
864if eq pid 0
865if ge fi 0
866r1 = dupfd fi 0 0
867end
868if ge fo 0
869r2 = dupfd fo 1 0
870end
871if ge fe 0
872r3 = dupfd fe 2 0
873end
874r4 = execprog m b rln REXEC
875exit 81
876end
877ret int pid
878end
879
880fn reap pid int to long
881sb = new uchar 4 0
882r = waitpid pid sb 0 4 0
883if ne r long pid
884ret -1
885end
886sg = and get sb 0 127
887if eq sg 0
888ret long get sb 1
889end
890ret add 256 long sg
891end
892
893fn alive pid int to bool
894sb = new uchar 4 0
895r = waitpid pid sb 0 4 1
896ret eq r 0
897end
898
899fn runw fi int fo int fe int to long
900pid = spawn fi fo fe
901if le pid 0
902ret -2
903end
904ret reap pid
905end
906
907note rules: run task.om (which 0) or the current mutant (which 1) on region ri; stdout to RFILE.
908fn rules which ulong ri ulong to long
909psc frin
910ck wfile ri frin
911fi = fopr
912psc frout
913fo = fopw 420
914ck and ge fi 0 ge fo 0 frout
915xbeg
916if eq which 0
917xarg 2
918else
919psc fmutb
920xreg RPATH
921end
922r = runw fi fo -1
923c1 = fclose fi
924c2 = fclose fo
925psc frout
926n = rfile RFILE
927ck ge n 0 frout
928ret r
929end
930
931note sqlrun: /usr/bin/sqlite3 on the database named by RDB, script RSQL, stdout to RFILE.
932fn sqlrun to long
933psc fsql
934ck wfile RSQL fsql
935fi = fopr
936psc fsqo
937fo = fopw 420
938ck and ge fi 0 ge fo 0 fsqo
939xbeg
940xa sqlbin
941xa sqbatch
942xa sqbail
943xreg RDB
944r = runw fi fo -1
945c1 = fclose fi
946c2 = fclose fo
947psc fsqo
948n = rfile RFILE
949ck ge n 0 fsqo
950ret r
951end
952
953note ---- cases mode
954fn case1 which ulong s ulong e ulong quiet bool to bool
955t1 = findc s e 9
956t2 = findc add t1 1 e 9
957ck lt t2 e fcases
958rsz RMISC
959apm RMISC add t1 1 sub sub t2 t1 1
960pb RMISC 10
961code = rules which RMISC
962w = add t2 1
963wn = sub e w
964ok = false
965if eq get m w 101
966want = pnum add w 4 e
967ok = and eq code long want eq rln RFILE 0
968else
969fb = get bs RFILE
970ok = and eq code 0 eq rln RFILE add wn 1
971if ok
972ok = same w fb wn
973end
974if ok
975ok = eq get m add fb wn 10
976end
977end
978if not quiet
979mark ok
980say tcase
981apm ROUT s sub t1 s
982if not ok
983say tcex
984apl ROUT code
985say tcout
986apm ROUT get bs RFILE rln RFILE
987end
988nl
989end
990ret ok
991end
992
993
994fn cases which ulong quiet bool to ulong
995pdir fcases
996n = rfile RCASE
997ck gt n 0 fcases
998p = get bs RCASE
999e = get cu RCASE
1000fails = ulong 0
1001cnt = ulong 0
1002while lt p e bound 65536
1003q = findc p e 10
1004if lt p q
1005if ne get m p 35
1006cnt = add cnt 1
1007if not case1 which p q quiet
1008fails = add fails 1
1009end
1010end
1011end
1012p = add q 1
1013end
1014if not quiet
1015say tcsum
1016apd ROUT cnt
1017say tsf
1018apd ROUT fails
1019nl
1020end
1021ret fails
1022end
1023
1024note ---- HTTP over Omega sockets
1025fn crlf r ulong to void
1026pb r 13
1027pb r 10
1028end
1029
1030fn hbuild rq ulong post bool path view uchar * to void
1031rsz rq
1032if post
1033ap rq tpost
1034else
1035ap rq tget
1036end
1037ap rq path
1038ap rq thttp
1039crlf rq
1040ap rq hhost
1041crlf rq
1042ap rq hauth
1043apr rq RTOK
1044crlf rq
1045ap rq hclose
1046crlf rq
1047if post
1048ap rq hctype
1049crlf rq
1050ap rq hclen
1051apd rq rln RBODY
1052crlf rq
1053end
1054crlf rq
1055if post
1056apr rq RBODY
1057end
1058end
1059
1060fn hconn port ulong to int
1061a = new uchar 16 0
1062put a 0 2
1063put a 2 uchar shr port 8
1064put a 3 uchar and port 255
1065put a 4 127
1066put a 7 1
1067fd = socknew 2 524289 0
1068if lt fd 0
1069ret -1
1070end
1071r = connect int fd a 0 16
1072if lt r 0
1073c = fclose int fd
1074ret -1
1075end
1076ret int fd
1077end
1078
1079fn hput fd int rq ulong to bool
1080b = get bs rq
1081n = rln rq
1082o = ulong 0
1083while lt o n bound 999999
1084k = send fd m add b o sub n o 0
1085if le k 0
1086ret false
1087end
1088o = add o ulong k
1089end
1090ret true
1091end
1092
1093fn crlf2 i ulong to bool
1094a = and eq get m i 13 eq get m add i 1 10
1095b = and eq get m add i 2 13 eq get m add i 3 10
1096ret and a b
1097end
1098
1099fn hstat to ulong
1100b = get bs RRESP
1101e = get cu RRESP
1102put g GBODY e
1103if lt sub e b 12
1104ret 0
1105end
1106code = pnum add b 9 add b 12
1107i = b
1108while lt add i 3 e bound 999999
1109if crlf2 i
1110put g GBODY add i 4
1111break
1112end
1113i = add i 1
1114end
1115if eq code NONE
1116ret 0
1117end
1118ret code
1119end
1120
1121note hrecv: read the whole response (the request said Connection: close); 30 s at most.
1122fn hrecv fd int to ulong
1123rsz RRESP
1124t0 = monons
1125p = get cu RRESP
1126lim = get lm RRESP
1127done = false
1128while not done bound 999999
1129w = waitio fd 1 100000000
1130if gt w 0
1131k = recv fd m p sub lim p 0
1132if le k 0
1133done = true
1134else
1135p = add p ulong k
1136end
1137end
1138if lt w 0
1139done = true
1140end
1141if gt sub monons t0 30000000000
1142done = true
1143end
1144if ge p lim
1145done = true
1146end
1147end
1148c = fclose fd
1149put cu RRESP p
1150code = hstat
1151put g GLAST code
1152ret code
1153end
1154
1155fn hreq slot ulong post bool path view uchar * to ulong
1156hbuild RREQ post path
1157fd = hconn get g add GPORTA slot
1158if lt fd 0
1159put g GLAST 0
1160put g GBODY get cu RRESP
1161ret 0
1162end
1163if not hput fd RREQ
1164c = fclose fd
1165put g GLAST 0
1166ret 0
1167end
1168ret hrecv fd
1169end
1170
1171fn hpost slot ulong path view uchar * to ulong
1172ret hreq slot true path
1173end
1174
1175fn hgetp slot ulong path view uchar * to ulong
1176ret hreq slot false path
1177end
1178
1179note ---- JSON: serde_json writes object keys in sorted order, so a top-level key such as
1180note "attempt_id" or "generation" comes before "task" and the first match is the top-level one.
1181fn jfind k view uchar * to ulong
1182e = get cu RRESP
1183n = len k
1184i = get g GBODY
1185while le add i n e bound 999999
1186if mtext i k
1187ret add i n
1188end
1189i = add i 1
1190end
1191ret NONE
1192end
1193
1194fn jnum k view uchar * to ulong
1195p = jfind k
1196if eq p NONE
1197ret NONE
1198end
1199ret pnum p get cu RRESP
1200end
1201
1202fn jis k view uchar * t view uchar * to bool
1203p = jfind k
1204if eq p NONE
1205ret false
1206end
1207if gt add p len t get cu RRESP
1208ret false
1209end
1210ret mtext p t
1211end
1212
1213note jatt: copy the response's attempt_id into slot s of RATT.
1214fn jatt s ulong to bool
1215o = add get bs RATT mul s 256
1216put g add GAL0 s 0
1217p = jfind kaid
1218if eq p NONE
1219ret false
1220end
1221q = findc p get cu RRESP 34
1222n = sub q p
1223if gt n 255
1224ret false
1225end
1226for i ulong n
1227put m add o i get m add p i
1228end
1229put g add GAL0 s n
1230ret true
1231end
1232
1233fn aidset s ulong to void
1234rsz RAID
1235apm RAID add get bs RATT mul s 256 get g add GAL0 s
1236end
1237
1238fn gen0 v ulong to ulong
1239if eq v NONE
1240ret 0
1241end
1242ret v
1243end
1244
1245note ---- request bodies
1246fn settid t view uchar * to void
1247rsz RTID
1248ap RTID t
1249end
1250
1251fn setart t view uchar * to void
1252rsz RART
1253ap RART t
1254end
1255
1256fn bopen to void
1257rsz RBODY
1258ap RBODY jtid
1259apr RBODY RTID
1260ap RBODY dq
1261if eq get g GCMD 1
1262ap RBODY jcmd
1263apr RBODY RCMD
1264ap RBODY dq
1265end
1266end
1267
1268fn bagent a view uchar * to void
1269ap RBODY jagent
1270ap RBODY a
1271ap RBODY dq
1272end
1273
1274fn bfence gn ulong to void
1275ap RBODY jaid
1276apr RBODY RAID
1277ap RBODY dq
1278ap RBODY jgenk
1279apd RBODY gn
1280end
1281
1282fn bttl t ulong to void
1283ap RBODY jttl
1284apd RBODY t
1285ap RBODY cbr
1286end
1287
1288fn bsub to void
1289ap RBODY jbr
1290apr RBODY RART
1291ap RBODY dq
1292ap RBODY cbr
1293end
1294
1295fn bver gn ulong vd ulong vf bool to void
1296ap RBODY jgenk
1297apd RBODY gn
1298ap RBODY jart
1299apr RBODY RART
1300ap RBODY dq
1301ap RBODY jvby
1302if vf
1303ap RBODY tver
1304end
1305ap RBODY dq
1306ap RBODY jvd
1307if eq vd 1
1308ap RBODY jtrue
1309else
1310ap RBODY jfalse
1311end
1312ap RBODY jnotes
1313end
1314
1315note claim: POST claim for RTID by agent a with ttl t; the attempt goes to slot s; returns
1316note the HTTP status, and the granted generation in g[GBODY]'s place is read by the caller.
1317fn claim slot ulong a view uchar * t ulong s ulong to ulong
1318bopen
1319bagent a
1320bttl t
1321code = hpost slot pclaim
1322ok = jatt s
1323ret code
1324end
1325
1326fn hbeat a view uchar * gn ulong t ulong to ulong
1327bopen
1328bagent a
1329bfence gn
1330bttl t
1331ret hpost 0 phb
1332end
1333
1334fn submit a view uchar * gn ulong to ulong
1335bopen
1336bagent a
1337bfence gn
1338bsub
1339ret hpost 0 psub
1340end
1341
1342fn verify gn ulong vd ulong vf bool to ulong
1343bopen
1344bver gn vd vf
1345ret hpost 0 pver
1346end
1347
1348note ---- command ids: while g[GCMD] is 1 every body carries "command_id": RCMD.
1349fn setcmd t view uchar * to void
1350rsz RCMD
1351ap RCMD t
1352put g GCMD 1
1353end
1354
1355fn nocmd to void
1356put g GCMD 0
1357end
1358
1359fn tpad n ulong to void
1360while lt rln RCMD n bound 256
1361pb RCMD 97
1362end
1363end
1364
1365fn reclaimc to ulong
1366rsz RBODY
1367ap RBODY jrcmd
1368apr RBODY RCMD
1369ap RBODY jrend
1370ret hpost 0 prec
1371end
1372
1373note bsave keeps the body of the last response; bsame compares the last response's body with it.
1374fn bsave to void
1375b = get g GBODY
1376rsz RSAVE
1377apm RSAVE b sub get cu RRESP b
1378end
1379
1380fn bsame to bool
1381b = get g GBODY
1382n = sub get cu RRESP b
1383if ne n rln RSAVE
1384ret false
1385end
1386ret same b get bs RSAVE n
1387end
1388
1389note rowsave keeps generation, attempt, lease and status of s10; rowsame compares them again.
1390note A schema without those columns (main) fails rowsame rather than stopping the driver.
1391fn rowq to bool
1392rsz RSQL
1393ap RSQL sqhd
1394ap RSQL trow10
1395ret eq sqlrun 0
1396end
1397
1398fn rowsave to void
1399ok = rowq
1400rsz RROW
1401if ok
1402apr RROW RFILE
1403end
1404end
1405
1406fn rowsame to bool
1407if not rowq
1408ret false
1409end
1410if ne rln RFILE rln RROW
1411ret false
1412end
1413ret same get bs RFILE get bs RROW rln RROW
1414end
1415
1416note ---- cockpit processes
1417fn freeport to ulong
1418a = new uchar 16 0
1419put a 0 2
1420put a 4 127
1421put a 7 1
1422fd = socknew 2 524289 0
1423ck ge fd 0 envbin
1424r = bind int fd a 0 16
1425ck eq r 0 envbin
1426k = sockname int fd a 0 16
1427ck ge k 0 envbin
1428prt = or shl ulong get a 2 8 ulong get a 3
1429c = fclose int fd
1430note never a port of the 18000s, where the live services listen
1431ck or lt prt 18000 gt prt 18999 envbin
1432ret prt
1433end
1434
1435fn cockenv prt ulong to void
1436rsz RMISC
1437ap RMISC ehome
1438apa RMISC 4
1439ap RMISC dhome
1440xreg RMISC
1441rsz RMISC
1442ap RMISC edb
1443apr RMISC RDB
1444xreg RMISC
1445rsz RMISC
1446ap RMISC etok
1447apr RMISC RTOK
1448xreg RMISC
1449rsz RMISC
1450ap RMISC eaddr
1451apd RMISC prt
1452xreg RMISC
1453end
1454
1455note cockexec: the argument list that starts cockpit binary argv[bin] on port prt.
1456note BRIDGE: execprog gives the program an empty environment, so /usr/bin/env sets the four
1457note variables of cockenv. When omega-mv adds the execprog variant that takes a packed environment
1458note list, this function is the only change: drop "xa envbin" and pass the cockenv records as
1459note that list.
1460fn cockexec bin ulong prt ulong to void
1461xbeg
1462xa envbin
1463cockenv prt
1464xarg bin
1465end
1466
1467note cockst starts cockpit binary argv[bin] as process slot and waits until it serves; false when
1468note the process exits first or does not serve within 60 s.
1469fn cockst slot ulong bin ulong to bool
1470prt = freeport
1471put g add GPORTA slot prt
1472cockexec bin prt
1473psc flog
1474lf = fopa
1475ck ge lf 0 flog
1476if eq slot 0
1477put g GT0 ulong monons
1478end
1479pid = spawn -1 lf lf
1480c = fclose lf
1481ck gt pid 0 flog
1482put g add GPIDA slot ulong pid
1483i = ulong 0
1484while lt i 600 bound 600
1485if not alive pid
1486put g add GPIDA slot 0
1487ret false
1488end
1489code = hgetp slot ptasks
1490if eq code 200
1491ret true
1492end
1493z = sleepns 100000000
1494i = add i 1
1495end
1496ret false
1497end
1498
1499fn cockup slot ulong bin ulong to void
1500ok = cockst slot bin
1501ck ok flog
1502end
1503
1504fn cockdn slot ulong to void
1505pid = int get g add GPIDA slot
1506if gt pid 0
1507r = killpid pid 15
1508s = reap pid
1509end
1510put g add GPIDA slot 0
1511end
1512
1513fn mktok to void
1514rb = new uchar 16 0
1515r = random rb 0 16 0
1516ck eq r 16 ttokp
1517rsz RTOK
1518ap RTOK ttokp
1519for i ulong 16
1520v = get rb i
1521pb RTOK hexd shr v 4
1522pb RTOK hexd and v 15
1523end
1524end
1525
1526fn sqtask t view uchar * to void
1527ap RSQL sqi1
1528ap RSQL t
1529ap RSQL sqi2
1530end
1531
1532note ---- http mode scenarios
1533fn screate to void
1534rsz RBODY
1535ap RBODY jcreate
1536code = hpost 0 ptasks
1537rep eq code 201 n_create
1538end
1539
1540fn st1 to void
1541settid ts1
1542setart asha0
1543code = verify 0 1 true
1544rep and eq code 409 jis kstat vopen n_t1
1545end
1546
1547fn st2t3 to void
1548settid ts2
1549c1 = claim 0 agw2 300 0
1550gn = gen0 jnum kgen
1551rep eq c1 200 n_t2a
1552aidset 0
1553setart asha2
1554c2 = submit agw2 gn
1555rep eq c2 200 n_t2b
1556c3 = verify gn 1 true
1557rep and eq c3 200 jis kstat vver n_t2c
1558c4 = claim 0 agw3 300 1
1559rep and eq c4 409 jis kstat vver n_t2
1560c5 = submit agw2 gn
1561rep and eq c5 409 jis kstat vver n_t3
1562end
1563
1564fn sverif to void
1565settid ts3
1566c1 = claim 0 agw3 300 0
1567gn = gen0 jnum kgen
1568rep eq c1 200 n_va
1569aidset 0
1570setart asha3
1571c2 = submit agw3 gn
1572rep and eq c2 200 and jis kartk vsha3 jis kbr vbr n_vb
1573setart ashax
1574c3 = verify gn 1 true
1575rep and eq c3 409 jis kstat vsub n_vc
1576setart asha3
1577c4 = verify add gn 1 1 true
1578rep and eq c4 409 jis kstat vsub n_vd
1579c5 = verify gn 1 false
1580rep eq c5 400 n_ve
1581c6 = verify gn 1 true
1582rep and eq c6 200 and jis kvby vver2 jis kvn vnotes n_vf
1583end
1584
1585fn sstale to void
1586settid ts4
1587c1 = claim 0 agwk 1 0
1588g1 = gen0 jnum kgen
1589rep eq c1 200 n_sa
1590z = sleepns 1500000000
1591c2 = claim 0 agwk 300 1
1592g2 = gen0 jnum kgen
1593rep and eq c2 200 eq g2 add g1 1 n_sb
1594aidset 0
1595c3 = hbeat agwk g1 300
1596rep eq c3 409 n_sc
1597setart asha0
1598c4 = submit agwk g1
1599rep eq c4 409 n_sd
1600aidset 1
1601c5 = hbeat agwk g2 300
1602rep eq c5 200 n_se
1603end
1604
1605fn sexpire to void
1606settid ts5
1607c1 = claim 0 agw5 1 0
1608gn = gen0 jnum kgen
1609rep eq c1 200 n_ea
1610aidset 0
1611z = sleepns 1500000000
1612c2 = hbeat agw5 gn 300
1613rep and eq c2 409 eq jnum kgen gn n_eb
1614setart asha0
1615c3 = submit agw5 gn
1616ok = and eq c3 409 eq jnum kgen gn
1617rsz RSQL
1618ap RSQL sqhd
1619ap RSQL tss5
1620r = sqlrun
1621rep and ok and eq r 0 not rtext RFILE tsubnl n_ec
1622end
1623
1624fn sexhaust to void
1625rsz RSQL
1626ap RSQL sqhd
1627ap RSQL tsx6
1628r = sqlrun
1629put g GLAST 0
1630rep eq r 0 n_xa
1631settid ts6
1632c1 = claim 0 agw6 300 0
1633rep eq c1 409 n_xb
1634settid ts7
1635c2 = claim 0 agw6 300 0
1636gn = jnum kgen
1637rep and eq c2 200 eq gn GMAX n_xc
1638aidset 0
1639c3 = hbeat agw6 gen0 gn 300
1640rep and eq c3 200 eq jnum kgen GMAX n_xd
1641end
1642
1643fn sttl to void
1644settid ts9
1645c1 = claim 0 agw9 0 0
1646rep and eq c1 400 jis kcode vinval n_ta
1647c2 = claim 0 agw9 86401 0
1648rep and eq c2 400 jis kcode vinval n_tb
1649c3 = claim 0 agw9 86400 0
1650gn = gen0 jnum kgen
1651rep eq c3 200 n_tc
1652aidset 0
1653c4 = hbeat agw9 gn 0
1654rep and eq c4 400 jis kcode vinval n_td
1655end
1656
1657note sjson: a claim body without agent_id, and a body that is not JSON, answer 400 invalid.
1658fn sjson to void
1659rsz RBODY
1660ap RBODY jbadm
1661c1 = hpost 0 pclaim
1662rep and eq c1 400 jis kcode vinval n_ja
1663rsz RBODY
1664ap RBODY jbadj
1665c2 = hpost 0 pclaim
1666rep and eq c2 400 jis kcode vinval n_jb
1667end
1668
1669fn le32 a view uchar * o ulong to int
1670v = or ulong get a o shl ulong get a add o 1 8
1671v = or v or shl ulong get a add o 2 16 shl ulong get a add o 3 24
1672ret int v
1673end
1674
1675note bheld: read sqlite3's stdout until "held" (4 bytes or more) arrives; 10 s at most.
1676fn bheld ro int to bool
1677lb = new uchar 4096 0
1678n = ulong 0
1679i = ulong 0
1680while lt i 100 bound 100
1681w = waitio ro 1 100000000
1682if gt w 0
1683k = read ro lb n sub 4096 n
1684if gt k 0
1685n = add n ulong k
1686end
1687end
1688if ge n 4
1689ret true
1690end
1691i = add i 1
1692end
1693ret false
1694end
1695
1696fn bclaim to void
1697settid ts8
1698t0 = monons
1699c = claim 0 agw9 300 0
1700dt = sub monons t0
1701ok = and eq c 503 and jis kcode vbusy jis kretry jtrue
1702say tbusyw
1703apd ROUT ulong div dt 1000000
1704sp ROUT
1705rep ok n_bb
1706end
1707
1708note holdon: sqlite3 on RDB runs script t and keeps its transaction open; true once it printed
1709note "held". holdoff rolls it back and reaps the process.
1710fn holdon t view uchar * to bool
1711pi = new uchar 8 0
1712po = new uchar 8 0
1713ck eq pipefds pi 0 8 524288 0 tbheld
1714ck eq pipefds po 0 8 524288 0 tbheld
1715xbeg
1716xa sqlbin
1717xa sqbatch
1718xreg RDB
1719pid = spawn le32 pi 0 le32 po 4 -1
1720z = fclose le32 pi 0
1721z = fclose le32 po 4
1722wi = le32 pi 4
1723ro = le32 po 0
1724put g GH ulong pid
1725put g add GH 1 ulong wi
1726put g add GH 2 ulong ro
1727z = write wi t 0 len t
1728ret bheld ro
1729end
1730
1731fn holdoff to void
1732wi = int get g add GH 1
1733z = write wi tbrb 0 len tbrb
1734z = fclose wi
1735z = reap int get g GH
1736z = fclose int get g add GH 2
1737end
1738
1739note sbusy: sqlite3 holds BEGIN IMMEDIATE on the database while the cockpit claims with a
1740note command id; the busy answer is not recorded, so the same id is granted after the release.
1741fn sbusy to void
1742ok = holdon tbsql
1743put g GLAST 0
1744rep ok n_ba
1745setcmd cidb1
1746bclaim
1747holdoff
1748c = claim 0 agw9 300 0
1749nocmd
1750rep eq c 200 n_bc
1751end
1752
1753note sreplay: claim, heartbeat, submit and verify of s10, each sent twice with one command id.
1754fn sreplay to void
1755settid ts10
1756setcmd cidc1
1757c1 = claim 0 agw2 300 0
1758gn = gen0 jnum kgen
1759rep eq c1 200 n_ra
1760bsave
1761rowsave
1762c2 = claim 0 agw2 300 1
1763rep and eq c2 200 bsame n_rb
1764rep rowsame n_rc
1765c3 = claim 0 agw2 301 1
1766rep and eq c3 409 jis kcode vcmdc n_rd
1767aidset 0
1768sreplay2 gn
1769end
1770
1771fn sreplay2 gn ulong to void
1772setcmd cidh1
1773c1 = hbeat agw2 gn 300
1774bsave
1775rowsave
1776z = sleepns 20000000
1777c2 = hbeat agw2 gn 300
1778rep and and eq c1 200 eq c2 200 and bsame rowsame n_re
1779setart asha2
1780setcmd cidu1
1781c3 = submit agw2 gn
1782bsave
1783c4 = submit agw2 gn
1784rep and and eq c3 200 eq c4 200 bsame n_rf
1785setcmd cidv1
1786c5 = verify gn 1 true
1787bsave
1788c6 = verify gn 1 true
1789rep and and eq c5 200 eq c6 200 bsame n_rg
1790nocmd
1791end
1792
1793note scmdid: id length 128 is accepted (verified s10 refuses the claim), 129 and a bad byte are
1794note 400 invalid; then a refused claim is not recorded, so its id is decided again later.
1795fn scmdid to void
1796settid ts10
1797rsz RCMD
1798ap RCMD cidp
1799tpad 128
1800put g GCMD 1
1801c1 = claim 0 agw2 300 0
1802rep and eq c1 409 jis kcode vrefus n_rj
1803settid ts11
1804tpad 129
1805c2 = claim 0 agw2 300 0
1806rep and eq c2 400 jis kcode vinval n_ri
1807setcmd cidbad
1808c3 = claim 0 agw2 300 0
1809rep and eq c3 400 jis kcode vinval n_rk
1810nocmd
1811c4 = claim 0 agw3 1 0
1812setcmd cidx1
1813c5 = claim 0 agw2 300 1
1814z = sleepns 1500000000
1815c6 = claim 0 agw2 300 1
1816nocmd
1817rep and and eq c4 200 eq c5 409 eq c6 200 n_rl
1818end
1819
1820fn sexp t view uchar * to bool
1821rsz RSQL
1822ap RSQL sqhd
1823ap RSQL texp1
1824ap RSQL t
1825ap RSQL tqend
1826ret eq sqlrun 0
1827end
1828
1829note srecl: a reclaim sweep with a command id, replayed after another lease expired, must not
1830note sweep again; then every recorded id is in forge_commands exactly once.
1831fn srecl to void
1832ok1 = sexp ts12
1833setcmd cidr1
1834c1 = reclaimc
1835bsave
1836ok2 = sexp ts13
1837c2 = reclaimc
1838nocmd
1839ok3 = sqlis tss13 tclnl
1840rep and and ok1 ok2 and and eq c1 200 eq c2 200 and bsame ok3 n_rh
1841put g GLAST 0
1842rep sqlis tcnt tcntw n_rm
1843end
1844
1845fn rtally ca ulong cb ulong to void
1846n = ulong 0
1847if eq ca 200
1848n = add n 1
1849end
1850if eq cb 200
1851n = add n 1
1852end
1853inc add GRZ n
1854if ne ca 200
1855loser ca
1856end
1857if ne cb 200
1858loser cb
1859end
1860end
1861
1862fn loser c ulong to void
1863if eq c 409
1864inc GLREF
1865ret
1866end
1867if eq c 503
1868inc GLBUSY
1869ret
1870end
1871inc GLOTH
1872end
1873
1874note srace: 400 trials; in each, two connections to two cockpit processes on one database file
1875note claim the same open task; both requests are sent before either response is read.
1876fn srace to void
1877cockup 1 3
1878rsz RSQL
1879ap RSQL sqhd
1880ap RSQL sqbeg
1881for i ulong 400
1882ap RSQL sqi1
1883pb RSQL 114
1884apd RSQL i
1885ap RSQL sqi2
1886end
1887ap RSQL sqcom
1888ck eq sqlrun 0 n_race
1889for i ulong 400
1890rsz RTID
1891pb RTID 114
1892apd RTID i
1893bopen
1894bagent agra
1895bttl 300
1896hbuild RREQ true pclaim
1897bopen
1898bagent agrb
1899bttl 300
1900hbuild RREQ2 true pclaim
1901fa = hconn get g GPORTA
1902fb = hconn add get g GPORTA 0
1903fb2 = hconn get g add GPORTA 1
1904if ge fb 0
1905c0 = fclose fb
1906end
1907if eq and i 1 0
1908oa = hput fa RREQ
1909ob = hput fb2 RREQ2
1910else
1911ob = hput fb2 RREQ2
1912oa = hput fa RREQ
1913end
1914ca = hrecv fa
1915cb = hrecv fb2
1916rtally ca cb
1917end
1918rsz RSQL
1919ap RSQL sqhd
1920ap RSQL trq
1921r = sqlrun
1922dbc = pnum get bs RFILE get cu RFILE
1923grants = add get g add GRZ 1 mul 2 get g add GRZ 2
1924put g GLAST 0
1925say trace1
1926apd ROUT get g add GRZ 1
1927say trace2
1928apd ROUT get g GRZ
1929say trace3
1930apd ROUT get g add GRZ 2
1931say trace4
1932apd ROUT get g GLREF
1933say trace5
1934apd ROUT get g GLBUSY
1935say trace6
1936apd ROUT get g GLOTH
1937say trace7
1938apd ROUT dbc
1939sp ROUT
1940ok = and and eq get g add GRZ 1 400 eq get g GLOTH 0 and eq r 0 eq dbc grants
1941rep ok n_race
1942end
1943
1944note scross: one command id sent to both cockpit processes replays the first answer.
1945fn scross to void
1946settid ts14
1947setcmd cidk1
1948c1 = claim 0 agw3 300 0
1949bsave
1950c2 = claim 1 agw3 300 1
1951rep and and eq c1 200 eq c2 200 bsame n_rx
1952c3 = claim 1 agw3 301 1
1953rep and eq c3 409 jis kcode vcmdc n_ry
1954nocmd
1955end
1956
1957fn rqtally ca ulong cb ulong to void
1958if and eq ca 200 eq cb 200
1959if bsame
1960inc GQ
1961ret
1962end
1963inc add GQ 2
1964ret
1965end
1966if or and eq ca 200 eq cb 503 and eq ca 503 eq cb 200
1967inc add GQ 1
1968ret
1969end
1970inc add GQ 2
1971end
1972
1973note sreprace: 40 trials; both cockpit processes get the identical claim with one command id at
1974note once. Each trial must answer the same body twice (or 200 and 503), with one record per id.
1975fn sreprace to void
1976rsz RSQL
1977ap RSQL sqhd
1978ap RSQL sqbeg
1979for i ulong 40
1980ap RSQL sqi1
1981pb RSQL 113
1982apd RSQL i
1983ap RSQL sqi2
1984end
1985ap RSQL sqcom
1986ck eq sqlrun 0 n_rr
1987for j ulong 40
1988rqtrial j
1989end
1990nocmd
1991put g GLAST 0
1992say trr1
1993apd ROUT get g GQ
1994say trr2
1995apd ROUT get g add GQ 1
1996say trr3
1997apd ROUT get g add GQ 2
1998sp ROUT
1999rep and eq get g add GQ 2 0 sqlis trq2 t4040 n_rr
2000end
2001
2002fn rqtrial i ulong to void
2003rsz RTID
2004pb RTID 113
2005apd RTID i
2006rsz RCMD
2007ap RCMD cidq
2008apd RCMD i
2009put g GCMD 1
2010bopen
2011bagent agra
2012bttl 300
2013hbuild RREQ true pclaim
2014hbuild RREQ2 true pclaim
2015fa = hconn get g GPORTA
2016fb = hconn get g add GPORTA 1
2017if eq and i 1 0
2018oa = hput fa RREQ
2019ob = hput fb RREQ2
2020else
2021ob = hput fb RREQ2
2022oa = hput fa RREQ
2023end
2024ca = hrecv fa
2025bsave
2026cb = hrecv fb
2027rqtally ca cb
2028end
2029
2030note slock: sqlite3 holds BEGIN IMMEDIATE while one request waits in the cockpit's busy handler
2031note for 2 s, longer than the 1 s ttl, then releases it. A claim must get a lease that is still
2032note live after it is granted; a heartbeat or submit whose lease ran out during the wait must be
2033note refused with the row unchanged, and its command id is not recorded, so a retry is refused too.
2034fn slock to void
2035slkc ts15 n_lk2
2036setcmd cidl1
2037slkc ts16 n_lk3
2038nocmd
2039slkh
2040slkhc
2041slks
2042end
2043
2044note lockreq: POST RBODY to path on cockpit 0 while sqlite3 holds the write lock for 2 s.
2045fn lockreq path view uchar * to ulong
2046ok = holdon tbsql
2047ck ok n_lk1
2048hbuild RREQ true path
2049fd = hconn get g GPORTA
2050ck ge fd 0 n_lk1
2051ok2 = hput fd RREQ
2052ck ok2 n_lk1
2053z = sleepns 2000000000
2054holdoff
2055ret hrecv fd
2056end
2057
2058fn sqtid pre view uchar * want view uchar * to bool
2059rsz RSQL
2060ap RSQL sqhd
2061ap RSQL pre
2062apr RSQL RTID
2063ap RSQL tqend
2064r = sqlrun
2065ret and eq r 0 rtext RFILE want
2066end
2067
2068fn rowqt to bool
2069rsz RSQL
2070ap RSQL sqhd
2071ap RSQL trowa
2072apr RSQL RTID
2073ap RSQL tqend
2074ret eq sqlrun 0
2075end
2076
2077fn rowsavt to void
2078ok = rowqt
2079rsz RROW
2080if ok
2081apr RROW RFILE
2082end
2083end
2084
2085fn rowsamet to bool
2086if not rowqt
2087ret false
2088end
2089if ne rln RFILE rln RROW
2090ret false
2091end
2092ret same get bs RFILE get bs RROW rln RROW
2093end
2094
2095fn slkc t view uchar * name view uchar * to void
2096settid t
2097bopen
2098bagent agw2
2099bttl 1
2100c = lockreq pclaim
2101rep and eq c 200 sqtid tlive tlivew name
2102end
2103
2104note lkclaim: a fresh 1 s lease on task t for w2, its fence kept in RAID and the row saved;
2105note NONE when the claim is not granted.
2106fn lkclaim t view uchar * to ulong
2107settid t
2108c = claim 0 agw2 1 0
2109if ne c 200
2110ret NONE
2111end
2112gn = gen0 jnum kgen
2113aidset 0
2114rowsavt
2115ret gn
2116end
2117
2118fn slkh to void
2119gn = lkclaim ts17
2120bopen
2121bagent agw2
2122bfence gn
2123bttl 300
2124c = lockreq phb
2125rep and and ne gn NONE eq c 409 rowsamet n_lk4
2126end
2127
2128fn slkhc to void
2129gn = lkclaim ts18
2130setcmd cidl2
2131bopen
2132bagent agw2
2133bfence gn
2134bttl 300
2135c1 = lockreq phb
2136c2 = hpost 0 phb
2137nocmd
2138ok = sqlis tlkcnt tlkcntw
2139rep and and and ne gn NONE eq c1 409 and eq c2 409 rowsamet ok n_lk5
2140end
2141
2142fn slks to void
2143gn = lkclaim ts19
2144setart asha2
2145bopen
2146bagent agw2
2147bfence gn
2148bsub
2149c = lockreq psub
2150rep and and ne gn NONE eq c 409 rowsamet n_lk6
2151end
2152
2153note scommit: a rollback-journal database with a reader holding its lock, so no commit can
2154note finish. The cockpit must refuse to serve it (the branch requires WAL mode and exits), and
2155note the task row must be unchanged. A cockpit that serves fails, whatever its claim answers.
2156fn scommit to void
2157rsz RDB
2158apa RDB 4
2159ap RDB fcomdb
2160cockup 0 3
2161rsz RSQL
2162ap RSQL sqhd
2163sqtask tc1
2164ck eq sqlrun 0 sqi1
2165cockdn 0
2166ok1 = sqlis tdelj tdelw
2167ok2 = holdon tbsql2
2168put g GLAST 0
2169rep and ok1 ok2 n_cfa
2170srv = cockst 0 3
2171code = ulong 0
2172if srv
2173settid tc1
2174code = claim 0 agw2 300 0
2175end
2176holdoff
2177cockdn 0
2178ok3 = sqlis tcomq tcopen
2179say tcom1
2180if srv
2181pb ROUT 49
2182else
2183pb ROUT 48
2184end
2185say tcom2
2186apd ROUT code
2187sp ROUT
2188put g GLAST code
2189rep and and ok1 ok2 and not srv ok3 n_cf
2190end
2191
2192fn shttp to void
2193rsz RDB
2194apa RDB 4
2195ap RDB fhttpdb
2196cockup 0 3
2197rsz RSQL
2198ap RSQL sqhd
2199sqtask ts1
2200sqtask ts2
2201sqtask ts3
2202sqtask ts4
2203sqtask ts5
2204sqtask ts6
2205sqtask ts7
2206sqtask ts8
2207sqtask ts9
2208sqtask ts10
2209sqtask ts11
2210sqtask ts12
2211sqtask ts13
2212sqtask ts14
2213sqtask ts15
2214sqtask ts16
2215sqtask ts17
2216sqtask ts18
2217sqtask ts19
2218ck eq sqlrun 0 sqi1
2219screate
2220st1
2221st2t3
2222sverif
2223sstale
2224sexpire
2225sexhaust
2226sttl
2227sjson
2228sbusy
2229sreplay
2230scmdid
2231srecl
2232srace
2233scross
2234sreprace
2235slock
2236cockdn 1
2237cockdn 0
2238scommit
2239end
2240
2241note ---- migrate mode
2242fn sqlis q view uchar * want view uchar * to bool
2243rsz RSQL
2244ap RSQL sqhd
2245ap RSQL q
2246r = sqlrun
2247ret and eq r 0 rtext RFILE want
2248end
2249
2250fn smig to void
2251rsz RDB
2252apa RDB 4
2253ap RDB fmigdb
2254cockup 0 7
2255rsz RSQL
2256ap RSQL sqhd
2257sqtask tm1
2258sqtask tm2
2259sqtask tm3
2260ck eq sqlrun 0 sqi1
2261settid tm2
2262c1 = claim 0 agold 600 0
2263rep eq c1 200 n_ma
2264settid tm3
2265c2 = claim 0 agold 600 0
2266setart asha0
2267rsz RAID
2268c3 = submit agold 0
2269rep and eq c2 200 eq c3 200 n_mb
2270cockdn 0
2271put g GLAST 0
2272rep sqlis tcols t13 n_mc
2273cockup 0 3
2274put g GLAST 0
2275rep sqlis tcols t21 n_md
2276rep sqlis tmrows tmwant n_me
2277settid tm2
2278c4 = claim 0 agnew 300 0
2279rep and eq c4 409 jis kstat vclaim n_mf
2280settid tm1
2281c5 = claim 0 agnew 300 0
2282rep and eq c5 200 eq jnum kgen 1 n_mg
2283settid tm3
2284c6 = claim 0 agnew 300 0
2285rep eq c6 200 n_mh
2286cockdn 0
2287put g GLAST 0
2288rep sqlis tcmdt t1nl n_mi
2289cockup 0 3
2290put g GLAST 0
2291rep and sqlis tcols t21 sqlis tcmdt t1nl n_mj
2292cockdn 0
2293end
2294
2295note ---- table mode
2296note dims: 0 state, 1 generation, 2 request generation (gen xor 0/1), 3 attempt matches,
2297note 4 artifact matches, 5 verdict, 6 lease (live, expired, none), 7 ttl.
2298fn fsz d ulong to ulong
2299if eq d 0
2300ret 6
2301end
2302if eq d 1
2303ret 4
2304end
2305if eq d 6
2306ret 3
2307end
2308if eq d 7
2309ret 4
2310end
2311ret 2
2312end
2313
2314note rmask: the dims an operation reads. claim: state gen lease ttl; renew: state gen rgen
2315note attempt lease ttl; submit: state gen rgen attempt lease; verify: state gen rgen artifact
2316note verdict; reap: state gen lease.
2317fn rmask op ulong to ulong
2318if eq op 1
2319ret 195
2320end
2321if eq op 2
2322ret 207
2323end
2324if eq op 3
2325ret 79
2326end
2327if eq op 4
2328ret 55
2329end
2330ret 67
2331end
2332
2333fn dsz op ulong d ulong to ulong
2334if eq and shr rmask op d 1 1
2335ret fsz d
2336end
2337ret 1
2338end
2339
2340fn opn op ulong to ulong
2341n = ulong 1
2342for d ulong 8
2343n = mul n dsz op d
2344end
2345ret n
2346end
2347
2348fn decode op ulong j ulong to void
2349for d ulong 8
2350s = dsz op d
2351put tc d rem j s
2352j = div j s
2353end
2354end
2355
2356fn stv i ulong to ulong
2357if lt i 5
2358ret i
2359end
2360ret 9
2361end
2362
2363fn genv i ulong to ulong
2364if eq i 0
2365ret 0
2366end
2367if eq i 1
2368ret 7
2369end
2370if eq i 2
2371ret sub GMAX 1
2372end
2373ret GMAX
2374end
2375
2376fn ttlv i ulong to ulong
2377if eq i 0
2378ret 0
2379end
2380if eq i 1
2381ret 1
2382end
2383if eq i 2
2384ret 86400
2385end
2386ret 86401
2387end
2388
2389fn expv i ulong now ulong to ulong
2390if eq i 0
2391ret add now 3600000
2392end
2393if eq i 1
2394ret sub now 3600000
2395end
2396ret 0
2397end
2398
2399fn apst r ulong s ulong to void
2400if eq s 0
2401ap r sopen
2402ret
2403end
2404if eq s 1
2405ap r sclaim
2406ret
2407end
2408if eq s 2
2409ap r ssub
2410ret
2411end
2412if eq s 3
2413ap r sver
2414ret
2415end
2416if eq s 4
2417ap r sfail
2418ret
2419end
2420ap r sunk
2421end
2422
2423fn stcode a ulong b ulong to ulong
2424n = sub b a
2425if eq n 4
2426if mtext a sopen
2427ret 0
2428end
2429end
2430if eq n 7
2431if mtext a sclaim
2432ret 1
2433end
2434if mtext a sunk
2435ret 9
2436end
2437end
2438if eq n 9
2439if mtext a ssub
2440ret 2
2441end
2442end
2443if eq n 8
2444if mtext a sver
2445ret 3
2446end
2447end
2448if eq n 6
2449if mtext a sfail
2450ret 4
2451end
2452end
2453ret 99
2454end
2455
2456note trow: the seed row x<k> and its rules input line, for the case decoded into tc.
2457fn trow op ulong k ulong now ulong to void
2458st = stv get tc 0
2459gn = genv get tc 1
2460ex = expv get tc 6 now
2461ap RSQL tins1
2462apd RSQL k
2463ap RSQL tins2
2464apst RSQL st
2465ap RSQL tins3
2466apd RSQL k
2467ap RSQL tins4
2468apd RSQL gn
2469ap RSQL tcomma
2470if eq get tc 6 2
2471ap RSQL tnull
2472else
2473apd RSQL ex
2474end
2475ap RSQL tins5
2476apd RSQL k
2477ap RSQL tins6
2478apd RTIN op
2479sp RTIN
2480apd RTIN st
2481sp RTIN
2482apd RTIN gn
2483sp RTIN
2484apd RTIN xor gn get tc 2
2485tin2 now ex
2486end
2487
2488fn tin2 now ulong ex ulong to void
2489sp RTIN
2490apd RTIN get tc 3
2491sp RTIN
2492apd RTIN get tc 4
2493sp RTIN
2494apd RTIN get tc 5
2495sp RTIN
2496apd RTIN now
2497sp RTIN
2498apd RTIN ex
2499sp RTIN
2500apd RTIN ttlv get tc 7
2501nlr RTIN
2502end
2503
2504fn tseed op ulong j0 ulong j1 ulong k0 ulong now ulong to void
2505rsz RSQL
2506ap RSQL sqhd
2507ap RSQL sqbeg
2508j = j0
2509while lt j j1 bound 4096
2510decode op j
2511trow op add k0 sub j j0 now
2512j = add j 1
2513end
2514ap RSQL sqcom
2515end
2516
2517note trreq: the request of operation op for row x<k> (case in tc); returns the HTTP status.
2518fn trreq op ulong k ulong to ulong
2519gn = genv get tc 1
2520rg = xor gn get tc 2
2521rsz RTID
2522pb RTID 120
2523apd RTID k
2524rsz RAID
2525if eq get tc 3 1
2526ap RAID tattp
2527apd RAID k
2528else
2529ap RAID tattx
2530end
2531rsz RART
2532if eq op 4
2533if eq get tc 4 1
2534ap RART tartp
2535apd RART k
2536else
2537ap RART tartx
2538end
2539else
2540ap RART tartn
2541end
2542bopen
2543if eq op 1
2544bagent agentc
2545bttl ttlv get tc 7
2546ret hpost 0 pclaim
2547end
2548if eq op 4
2549bver rg get tc 5 true
2550ret hpost 0 pver
2551end
2552bagent agentt
2553bfence rg
2554if eq op 2
2555bttl ttlv get tc 7
2556ret hpost 0 phb
2557end
2558bsub
2559ret hpost 0 psub
2560end
2561
2562fn tops op ulong j0 ulong j1 ulong k0 ulong to void
2563if eq op 5
2564rsz RBODY
2565code = hpost 0 prec
2566put tr 0 code
2567put g GRECL jnum krecl
2568ret
2569end
2570j = j0
2571while lt j j1 bound 4096
2572decode op j
2573put tr sub j j0 trreq op add k0 sub j j0
2574j = add j 1
2575end
2576end
2577
2578note tdump: one observation line per row, "refused next_state next_generation"; refused is 0
2579note for HTTP 200, 1 for 409, 3 for 400, 2 for anything else. A reap is observed by its row alone.
2580fn tdump op ulong j0 ulong j1 ulong to void
2581rsz RSQL
2582ap RSQL sqhd
2583ap RSQL tdump_
2584ck eq sqlrun 0 tdump_
2585p = get bs RFILE
2586e = get cu RFILE
2587j = j0
2588while lt j j1 bound 4096
2589ck lt p e tdump_
2590q = findc p e 10
2591a = findc p q 124
2592b = findc add a 1 q 124
2593sc = stcode add a 1 b
2594gn = pnum add b 1 q
2595obs = tobs op j sc
2596apd RTOBS obs
2597sp RTOBS
2598apd RTOBS sc
2599sp RTOBS
2600apd RTOBS gn
2601nlr RTOBS
2602p = add q 1
2603j = add j 1
2604end
2605end
2606
2607fn tobs op ulong j ulong sc ulong to ulong
2608if eq op 5
2609decode op j
2610if and eq stv get tc 0 1 eq sc 0
2611inc GRALW
2612ret 0
2613end
2614ret 1
2615end
2616c = get tr sub j get g GP1
2617if eq c 200
2618ret 0
2619end
2620if eq c 409
2621ret 1
2622end
2623if eq c 400
2624ret 3
2625end
2626ret 2
2627end
2628
2629fn tchunk op ulong j0 ulong j1 ulong to void
2630ch = get g GTCH
2631inc GTCH
2632rsz RDB
2633apa RDB 4
2634ap RDB ftbl
2635apd RDB ch
2636ap RDB fdbs
2637cockup 0 3
2638now = div ulong realns 1000000
2639k0 = get g GROW
2640tseed op j0 j1 k0 now
2641ck eq sqlrun 0 tins1
2642tops op j0 j1 k0
2643el = sub ulong monons get g GT0
2644if ge el 25000000000
2645say tinc
2646apd ROUT ch
2647say tinc2
2648flush
2649inc GINC
2650end
2651put g GP1 j0
2652tdump op j0 j1
2653put g GROW add k0 sub j1 j0
2654cockdn 0
2655end
2656
2657note ---- table op 7: command ids. Row j has dims P = j%2 (a prior request with the id),
2658note I = (j/2)%5 (no id, short id, 128 bytes, 129 bytes, a '/'), M = (j/10)%2 (the prior request
2659note was the identical claim of k<k>, or a claim of the decoy y<k>), T = j/20 (k<k> open,
2660note verified, absent). The rules line is "7 prior idlen bad M has 0 0 0 sdec" and the observed
2661note line is "http records changed".
2662fn cdim j ulong d ulong to ulong
2663if eq d 0
2664ret rem j 2
2665end
2666if eq d 1
2667ret rem div j 2 5
2668end
2669if eq d 2
2670ret rem div j 10 2
2671end
2672ret div j 20
2673end
2674
2675fn tcid j ulong k ulong to void
2676iv = cdim j 1
2677rsz RCMD
2678put g GCMD 0
2679if eq iv 0
2680ret
2681end
2682put g GCMD 1
2683pb RCMD 99
2684apd RCMD k
2685if eq iv 4
2686ap RCMD tslx
2687end
2688if eq iv 2
2689tpad 128
2690end
2691if eq iv 3
2692tpad 129
2693end
2694end
2695
2696fn tcrow j ulong k ulong to void
2697tt = cdim j 3
2698ap RSQL sqi1
2699pb RSQL 121
2700apd RSQL k
2701ap RSQL sqi2
2702if eq tt 2
2703ret
2704end
2705ap RSQL sqi1
2706pb RSQL 107
2707apd RSQL k
2708ap RSQL sqi2
2709if eq tt 1
2710ap RSQL tkupd1
2711apd RSQL k
2712ap RSQL tqend
2713end
2714end
2715
2716fn tcseed k0 ulong to void
2717rsz RSQL
2718ap RSQL sqhd
2719ap RSQL tcmdg
2720ap RSQL sqbeg
2721for j ulong 60
2722tcrow j add k0 j
2723end
2724ap RSQL sqcom
2725end
2726
2727fn tcclaim to ulong
2728bopen
2729bagent agentc
2730bttl 300
2731ret hpost 0 pclaim
2732end
2733
2734fn tcph1 j ulong k ulong to void
2735if or eq cdim j 0 0 eq cdim j 1 0
2736ret
2737end
2738tcid j k
2739rsz RTID
2740if eq cdim j 2 1
2741pb RTID 107
2742else
2743pb RTID 121
2744end
2745apd RTID k
2746c = tcclaim
2747end
2748
2749fn tcreq j ulong k ulong to void
2750tcid j k
2751rsz RTID
2752pb RTID 107
2753apd RTID k
2754put tk add mul j 4 3 tcclaim
2755end
2756
2757fn tcq k ulong to void
2758ap RSQL td1
2759apd RSQL k
2760ap RSQL td2
2761apd RSQL k
2762ap RSQL td3
2763if eq get g GCMD 1
2764apr RSQL RCMD
2765end
2766ap RSQL td4
2767end
2768
2769note tsd: the status code of stcode as the state decision of op 7 (open 0, claimed by a live
2770note lease 3 HELD, verified 2 BADSTATE, absent 1 NOTFOUND).
2771fn tsd s ulong to ulong
2772if eq s 0
2773ret 0
2774end
2775if eq s 1
2776ret 3
2777end
2778if eq s 3
2779ret 2
2780end
2781ret 1
2782end
2783
2784fn tcin j ulong s1 ulong c1 ulong to void
2785tcid j add get g GK0 j
2786iv = cdim j 1
2787has = ulong 1
2788if eq iv 0
2789has = 0
2790end
2791bad = ulong 0
2792if eq iv 4
2793bad = 1
2794end
2795apd RTIN 7
2796sp RTIN
2797apd RTIN c1
2798sp RTIN
2799apd RTIN rln RCMD
2800sp RTIN
2801apd RTIN bad
2802sp RTIN
2803apd RTIN cdim j 2
2804sp RTIN
2805apd RTIN has
2806ap RTIN tz3
2807apd RTIN tsd s1
2808nlr RTIN
2809end
2810
2811fn tcobs j ulong sc ulong gn ulong cn ulong to void
2812o = mul j 4
2813s1 = get tk o
2814chg = ulong 0
2815if or ne sc s1 ne gn get tk add o 1
2816chg = 1
2817end
2818apd RTOBS get tk add o 3
2819sp RTOBS
2820apd RTOBS cn
2821sp RTOBS
2822apd RTOBS chg
2823nlr RTOBS
2824tcin j s1 get tk add o 2
2825end
2826
2827fn tcline j ulong p ulong q ulong ph ulong to void
2828a = findc p q 124
2829b = findc add a 1 q 124
2830sc = stcode p a
2831gn = pnum add a 1 b
2832cn = pnum add b 1 q
2833if eq ph 0
2834o = mul j 4
2835put tk o sc
2836put tk add o 1 gn
2837put tk add o 2 cn
2838ret
2839end
2840tcobs j sc gn cn
2841end
2842
2843fn tcdump ph ulong to void
2844k0 = get g GK0
2845rsz RSQL
2846ap RSQL sqhd
2847for j ulong 60
2848tcid j add k0 j
2849tcq add k0 j
2850end
2851ck eq sqlrun 0 td1
2852p = get bs RFILE
2853e = get cu RFILE
2854for j2 ulong 60
2855ck lt p e td1
2856q = findc p e 10
2857tcline j2 p q ph
2858p = add q 1
2859end
2860end
2861
2862fn tcmd to void
2863ch = get g GTCH
2864inc GTCH
2865rsz RDB
2866apa RDB 4
2867ap RDB ftbl
2868apd RDB ch
2869ap RDB fdbs
2870cockup 0 3
2871k0 = get g GROW
2872put g GK0 k0
2873tcseed k0
2874ck eq sqlrun 0 tcmdg
2875for j ulong 60
2876tcph1 j add k0 j
2877end
2878tcdump 0
2879for j2 ulong 60
2880tcreq j2 add k0 j2
2881end
2882tcdump 1
2883put g GROW add k0 60
2884nocmd
2885cockdn 0
2886end
2887
2888fn httpof dec ulong sdec ulong to ulong
2889if eq dec 11
2890ret 200
2891end
2892if eq dec 12
2893ret 409
2894end
2895if eq dec 13
2896ret 400
2897end
2898if eq sdec 0
2899ret 200
2900end
2901if eq sdec 1
2902ret 404
2903end
2904if eq sdec 8
2905ret 400
2906end
2907if eq sdec 9
2908ret 503
2909end
2910if eq sdec 10
2911ret 500
2912end
2913ret 409
2914end
2915
2916note tfield: number idx (from 0) of the rules input line at tp..tq.
2917fn tfield tp ulong tq ulong idx ulong to ulong
2918put g GP4 tp
2919put g add GP4 1 tq
2920v = ulong 0
2921r = ulong 0
2922while le r idx bound 16
2923v = nxt GP4
2924r = add r 1
2925end
2926ret v
2927end
2928
2929note trowok: op 7 compares HTTP status, record count and change; the other ops compare the
2930note refusal class (0 granted, 3 invalid 400, 1 refused), next state and next generation.
2931fn trowok tp ulong tq ulong to bool
2932if eq tfield tp tq 0 7
2933ex = httpof get tv 0 tfield tp tq 9
2934if ne ex get tv 3
2935ret false
2936end
2937if ne get tv 1 get tv 4
2938ret false
2939end
2940ret eq get tv 2 get tv 5
2941end
2942ra = ulong 1
2943if eq get tv 0 0
2944ra = 0
2945end
2946if eq get tv 0 8
2947ra = 3
2948end
2949ok = eq ra get tv 3
2950if ne get tv 1 get tv 4
2951ok = false
2952end
2953if ne get tv 2 get tv 5
2954ok = false
2955end
2956ret ok
2957end
2958
2959note trowck: tv holds the 3 rules outputs, then the 3 observed values of the row.
2960fn trowck i ulong verbose bool bad ulong to bool
2961for j ulong 3
2962put tv j nxt GP1
2963end
2964for j2 ulong 3
2965put tv add j2 3 nxt GP2
2966end
2967tp = get g GP3
2968tq = findc tp get g add GP3 1 10
2969put g GP3 add tq 1
2970ok = trowok tp tq
2971if and not ok and verbose lt bad 40
2972trowpr i tp tq
2973end
2974ret ok
2975end
2976
2977fn trowpr i ulong tp ulong tq ulong to void
2978say tfrow
2979apd ROUT i
2980say tfin
2981apm ROUT tp sub tq tp
2982say tfrul
2983for j ulong 6
2984if eq j 3
2985say tfobs
2986else
2987sp ROUT
2988end
2989apd ROUT get tv j
2990end
2991nl
2992end
2993
2994note tcheck: run the rules (which 0) or the mutant (which 1) on every table row and count the
2995note rows whose decision, next state or next generation differ from what the cockpit did.
2996fn tcheck which ulong verbose bool to ulong
2997rows = get g GROW
2998code = rules which RTIN
2999if ne code 0
3000ret add rows 1
3001end
3002put g GP1 get bs RFILE
3003put g add GP1 1 get cu RFILE
3004put g GP2 get bs RTOBS
3005put g add GP2 1 get cu RTOBS
3006put g GP3 get bs RTIN
3007put g add GP3 1 get cu RTIN
3008bad = ulong 0
3009i = ulong 0
3010while lt i rows bound 65536
3011if not trowck i verbose bad
3012bad = add bad 1
3013end
3014i = add i 1
3015end
3016ret bad
3017end
3018
3019fn mfind fr view uchar * occ ulong to ulong
3020p = get bs RSRC
3021e = get cu RSRC
3022seen = ulong 0
3023while lt p e bound 999999
3024q = findc p e 10
3025if eq sub q p len fr
3026if mtext p fr
3027seen = add seen 1
3028if eq seen occ
3029ret p
3030end
3031end
3032end
3033p = add q 1
3034end
3035ret NONE
3036end
3037
3038fn mutant fr view uchar * tx view uchar * occ ulong to void
3039pos = mfind fr occ
3040if eq pos NONE
3041say tmuta
3042say fr
3043nl
3044inc GFAIL
3045ret
3046end
3047rsz RMUT
3048b = get bs RSRC
3049apm RMUT b sub pos b
3050ap RMUT tx
3051q = add pos len fr
3052apm RMUT q sub get cu RSRC q
3053psc fmuts
3054ck wfile RMUT fmuts
3055fi = fopr
3056psc fmutb
3057fo = fopw 493
3058ck and ge fi 0 ge fo 0 fmutb
3059xbeg
3060xarg 6
3061code = runw fi fo -1
3062c1 = fclose fi
3063c2 = fclose fo
3064if ne code 0
3065say tmutc
3066say tx
3067nl
3068inc GFAIL
3069ret
3070end
3071cf = cases 1 true
3072tf = tcheck 1 false
3073mutrep tx occ cf tf
3074end
3075
3076fn mutrep tx view uchar * occ ulong cf ulong tf ulong to void
3077killed = or gt cf 0 gt tf 0
3078if killed
3079say tpass
3080inc GPASS
3081else
3082say tfail
3083inc GFAIL
3084end
3085say tmut
3086say tx
3087sp ROUT
3088apd ROUT occ
3089if killed
3090say tmutk
3091else
3092say tmuts
3093end
3094apd ROUT cf
3095say tmutt
3096apd ROUT tf
3097nl
3098end
3099
3100fn smuts to void
3101pdir fsrc
3102n = rfile RSRC
3103ck gt n 0 fsrc
3104mutant mf01 mt01 1
3105mutant mf02 mt02 1
3106mutant mf02 mt02 2
3107mutant mf04 mt04 1
3108mutant mf05 mt05 1
3109mutant mf05 mt06 1
3110mutant mf07 mt07 1
3111mutant mf08 mt08 1
3112mutant mf08 mt09 1
3113mutant mf10 mt10 1
3114mutant mf11 mt11 1
3115mutant mf12 mt12 1
3116mutant mf13 mt13 1
3117mutant mf14 mtfal 1
3118mutant mf15 mt15 1
3119mutant mf16 mtfal 1
3120mutant mf16 mtfal 2
3121mutant mf17 mtfal 1
3122mutant mf18 mtfal 1
3123mutant mf19 mtfal 1
3124mutant mf20 mtfal 1
3125mutant mf21 mt21 1
3126mutant mf22 mtfal 1
3127mutant mf23 mtfal 1
3128mutant mf24 mt24 1
3129mutant mf25 mt25 1
3130mutant mf26 mtfal 1
3131mutant mf26 mtfal 2
3132mutant mf27 mtfal 1
3133end
3134
3135fn stable to void
3136put g GROW 0
3137for o ulong 5
3138op = add o 1
3139n = opn op
3140j0 = ulong 0
3141while lt j0 n bound 64
3142j1 = add j0 400
3143if gt j1 n
3144j1 = n
3145end
3146tchunk op j0 j1
3147j0 = j1
3148end
3149end
3150tcmd
3151psc ftin
3152ck wfile RTIN ftin
3153psc ftobs
3154ck wfile RTOBS ftobs
3155bad = tcheck 0 true
3156put g GLAST 0
3157say ttab1
3158apd ROUT get g GROW
3159say ttab2
3160apd ROUT bad
3161say ttab3
3162apd ROUT get g GTCH
3163sp ROUT
3164rep eq bad 0 n_tab
3165say ttab4
3166apd ROUT get g GRECL
3167say ttab5
3168apd ROUT get g GRALW
3169sp ROUT
3170rep eq get g GRECL get g GRALW n_trec
3171smuts
3172end
3173
3174fn main to int
3175setup
3176args
3177mktok
3178psc dhome
3179r = makedir m get bs RPATH rln RPATH 493
3180if argis 1 mcases
3181f = cases 0 false
3182end
3183if argis 1 mhttp
3184shttp
3185end
3186if argis 1 mtable
3187stable
3188end
3189if argis 1 mmig
3190smig
3191end
3192say tsumm
3193apa ROUT 1
3194say tsp
3195apd ROUT get g GPASS
3196say tsf
3197apd ROUT get g GFAIL
3198say tsn
3199apd ROUT get g GNRUN
3200say tsi
3201apd ROUT get g GINC
3202nl
3203if gt add get g GPASS get g GFAIL 0
3204if eq get g GFAIL 0
3205ret 0
3206end
3207end
3208ret 1
3209end