code wiki / _hdl_build / nx_buildq_gate.nx
nx_buildq_gate.nx source
↩ module page · 144 lines · 10035 B
1// nx_buildq_gate.nx -- the REFEREE for nx_buildq (deploy DL9 bq_dedupe_target, 2026-09-02). Drives the built
2// organ against the REAL nx_store_put on fixture planes under /tmp and asserts: one target enqueued twice is ONE
3// row (by id) and the second add SAYS so; `close` turns a queued row closed in place with its reason; a fired row
4// is left alone; nothing to close is a named NO-ROW with exit 0; an unsafe target is refused; and the pure row
5// finder matches the exact id (BQ-t1 never matches BQ-t10). Every plane assertion re-reads the plane through
6// the store verb, never the organ's receipt.
7// nx_buildq_gate [subject-elf] [store-elf] defaults _build/nx_buildq.sov.elf and _build/nx_store_put.sov.elf
8// license_tier: ORIGINAL. Writes only under /tmp/nx_buildq_gate/. No hw writes.
9import "nx_syscalls.nx"
10import "nx_gate_verdict.nx"
11import "nx_gatekit_lib.nx"
12import "nx_tool_run.nx"
13
14const QG_CAP: i64 = 262144
15const QG_I64: i64 = 8
16const QG_ARGV: i64 = 16
17const QG_TIMEOUT_MS: i64 = 60000
18const QG_ROOT: *u8 = "/tmp/nx_buildq_gate"
19const QG_QUEUE: *u8 = "/tmp/nx_buildq_gate/deployq-"
20const QG_PLAN: *u8 = "/tmp/nx_buildq_gate/plan-build-"
21
22func qg_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, a6: *u8, a7: *u8, a8: *u8, a9: *u8, out: *u8, outlen: *i64) -> i64 {
23 let av: *i64 = sys_mmap(QG_ARGV * QG_I64) as *i64
24 av[0] = elf as i64; av[1] = a1 as i64; av[2] = a2 as i64; av[3] = a3 as i64; av[4] = a4 as i64; av[5] = a5 as i64
25 av[6] = a6 as i64; av[7] = a7 as i64; av[8] = a8 as i64; av[9] = a9 as i64
26 var k: i64 = 1
27 var z: i64 = 0
28 while k < 10 { if z == 0 { if gk_len(av[k] as *u8) == 0 { av[k] = 0; z = 1 } } else { av[k] = 0 } k = k + 1 }
29 av[10] = 0
30 let rc: i64 = tr_run_capture_to(elf, av, out, QG_CAP, outlen, QG_TIMEOUT_MS)
31 let n: i64 = outlen[0]
32 if n >= 0 { if n < QG_CAP { out[n] = 0 as u8 } }
33 return rc
34}
35// the plane, loaded through the store verb itself
36func qg_load(store: *u8, out: *u8, outlen: *i64) -> i64 {
37 return qg_run(store, QG_QUEUE, "load" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, outlen)
38}
39// how many lines of the dump start with `id` followed by a TAB
40func qg_rows_with_id(dump: *u8, n: i64, id: *u8) -> i64 {
41 let il: i64 = gk_len(id)
42 var count: i64 = 0
43 var i: i64 = 0
44 while i < n {
45 let e: i64 = gk_eol(dump, i, n)
46 if e - i > il {
47 var same: i64 = 1
48 var k: i64 = 0
49 while k < il { if dump[i + k] != id[k] { same = 0 } k = k + 1 }
50 if same == 1 { if dump[i + il] == 9 as u8 { count = count + 1 } }
51 }
52 i = e + 1
53 }
54 return count
55}
56// the status field (4th, tab-delimited) of the line whose id is `id`, copied into st; 1 when found
57func qg_status_of(dump: *u8, n: i64, id: *u8, st: *u8, cap: i64) -> i64 {
58 let il: i64 = gk_len(id)
59 var i: i64 = 0
60 while i < n {
61 let e: i64 = gk_eol(dump, i, n)
62 if e - i > il {
63 var same: i64 = 1
64 var k: i64 = 0
65 while k < il { if dump[i + k] != id[k] { same = 0 } k = k + 1 }
66 if same == 1 { if dump[i + il] == 9 as u8 {
67 var tabs: i64 = 0
68 var p: i64 = i
69 while p < e { if dump[p] == 9 as u8 { tabs = tabs + 1; if tabs == 3 { var c: i64 = 0; var q: i64 = p + 1; while q < e { if dump[q] == 9 as u8 { q = e } else { if c < cap - 1 { st[c] = dump[q]; c = c + 1 } q = q + 1 } } st[c] = 0 as u8; return 1 } } p = p + 1 }
70 st[0] = 0 as u8
71 return 1
72 } }
73 }
74 i = e + 1
75 }
76 st[0] = 0 as u8
77 return 0
78}
79
80func main(argc: i64, argv: *i64) -> i64 {
81 var elf: *u8 = "_build/nx_buildq.sov.elf" as *u8
82 var store: *u8 = "_build/nx_store_put.sov.elf" as *u8
83 if argc >= 2 { elf = argv[1] as *u8 }
84 if argc >= 3 { store = argv[2] as *u8 }
85 gv_head("NX-BUILDQ-GATE: DL9 -- one queued row per target, said out loud, and closed in place when another door built it" as *u8)
86 let ctr: *i64 = gv_ctr()
87 gv_need("subject elf present" as *u8, gk_exists(elf), ctr)
88 gv_need("store verb present" as *u8, gk_exists(store), ctr)
89 // a fresh fixture plane every run: the store's own segment files live under the prefix's directory
90 gk_mkdir(QG_ROOT)
91 let out: *u8 = sys_mmap(QG_CAP)
92 let ol: *i64 = sys_mmap(QG_I64) as *i64
93 let dump: *u8 = sys_mmap(QG_CAP)
94 let dl: *i64 = sys_mmap(QG_I64) as *i64
95 let st: *u8 = sys_mmap(256)
96
97 // ---- T1 first add: one queued row ----
98 let rc1: i64 = qg_run(elf, "add" as *u8, "t1" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, "" as *u8, out, ol)
99 gv_check("T1 add seeds the queue row and reports QUEUED with exit 0" as *u8, gk_out_has(out, ol[0], "BUILDQ QUEUED id=BQ-t1" as *u8) * (rc1 == 0), ctr)
100 qg_load(store, dump, dl)
101 gv_check("T2 the plane holds exactly one BQ-t1 row and its status is queued (read through the store, not the receipt)" as *u8, (qg_rows_with_id(dump, dl[0], "BQ-t1" as *u8) == 1) * qg_status_of(dump, dl[0], "BQ-t1" as *u8, st, 256) * gk_streq(st, "queued" as *u8), ctr)
102
103 // ---- T3 second add of the same target: still one row, and the organ SAYS it replaced it ----
104 let rc3: i64 = qg_run(elf, "add" as *u8, "t1" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, "" as *u8, out, ol)
105 qg_load(store, dump, dl)
106 gv_check("T3 a second add of one target names the existing row (ALREADY-QUEUED) and the plane still holds ONE BQ-t1 row" as *u8, gk_out_has(out, ol[0], "BUILDQ ALREADY-QUEUED id=BQ-t1 status=queued" as *u8) * (rc3 == 0) * (qg_rows_with_id(dump, dl[0], "BQ-t1" as *u8) == 1), ctr)
107
108 // ---- T4 an unrelated target with a longer name must not alias (exact id match) ----
109 qg_run(elf, "add" as *u8, "t10" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, "" as *u8, out, ol)
110 qg_load(store, dump, dl)
111 gv_check("T4 BQ-t1 and BQ-t10 are two rows: the finder matches the exact id, never a prefix" as *u8, (qg_rows_with_id(dump, dl[0], "BQ-t1" as *u8) == 1) * (qg_rows_with_id(dump, dl[0], "BQ-t10" as *u8) == 1), ctr)
112
113 // ---- T5 close: queued -> closed in place, reason carried ----
114 let rc5: i64 = qg_run(elf, "close" as *u8, "t1" as *u8, "0" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "" as *u8, "" as *u8, out, ol)
115 qg_load(store, dump, dl)
116 gv_check("T5 close turns the queued row closed IN PLACE (still one row) and says so, exit 0" as *u8, gk_out_has(out, ol[0], "BUILDQ CLOSED id=BQ-t1" as *u8) * (rc5 == 0) * (qg_rows_with_id(dump, dl[0], "BQ-t1" as *u8) == 1) * qg_status_of(dump, dl[0], "BQ-t1" as *u8, st, 256) * gk_streq(st, "closed" as *u8), ctr)
117 gv_check("T6 the closed row carries the reason and the rc in its note" as *u8, gk_out_has(dump, dl[0], "closed by nx_buildq close: a build of t1 succeeded by another door rc=0" as *u8), ctr)
118 gv_check("T7 the unrelated queued row (BQ-t10) is untouched by the close" as *u8, qg_status_of(dump, dl[0], "BQ-t10" as *u8, st, 256) * gk_streq(st, "queued" as *u8), ctr)
119
120 // ---- T8 close again: a closed row is left as its own record ----
121 let rc8: i64 = qg_run(elf, "close" as *u8, "t1" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "" as *u8, "" as *u8, "" as *u8, out, ol)
122 gv_check("T8 closing a row that is not queued is a named NOT-QUEUED with exit 0 (a fired or closed row keeps its record)" as *u8, gk_out_has(out, ol[0], "BUILDQ CLOSE NOT-QUEUED id=BQ-t1 status=closed" as *u8) * (rc8 == 0), ctr)
123
124 // ---- T9 nothing to close ----
125 let rc9: i64 = qg_run(elf, "close" as *u8, "never_queued" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "" as *u8, "" as *u8, "" as *u8, out, ol)
126 gv_check("neg-control-nothing-to-close-is-a-named-NO-ROW-with-exit-0 (a build lane must not fail because nobody queued it)" as *u8, gk_out_has(out, ol[0], "BUILDQ CLOSE NO-ROW id=BQ-never_queued" as *u8) * (rc9 == 0), ctr)
127
128 // ---- T10 a closed row does not block a fresh enqueue ----
129 let rc10: i64 = qg_run(elf, "add" as *u8, "t1" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, "" as *u8, out, ol)
130 qg_load(store, dump, dl)
131 gv_check("T10 after a close the same target can be queued again: ALREADY-QUEUED names status=closed, the row reads queued again, still one row" as *u8, gk_out_has(out, ol[0], "BUILDQ ALREADY-QUEUED id=BQ-t1 status=closed" as *u8) * (rc10 == 0) * (qg_rows_with_id(dump, dl[0], "BQ-t1" as *u8) == 1) * qg_status_of(dump, dl[0], "BQ-t1" as *u8, st, 256) * gk_streq(st, "queued" as *u8), ctr)
132
133 // ---- refusals ----
134 let rc11: i64 = qg_run(elf, "add" as *u8, "../etc" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, "" as *u8, out, ol)
135 gv_check("neg-control-an-unsafe-target-is-refused-before-any-plane-write (exit 4)" as *u8, gk_out_has(out, ol[0], "BUILDQ REFUSED: target must be a bare organ basename" as *u8) * (rc11 == 4), ctr)
136 let rc12: i64 = qg_run(elf, "add" as *u8, "t2" as *u8, "12x" as *u8, "--store" as *u8, store, "--queue" as *u8, QG_QUEUE, "--plan" as *u8, QG_PLAN, out, ol)
137 gv_check("neg-control-a-non-decimal-numeric-argument-is-refused (exit 4)" as *u8, gk_out_has(out, ol[0], "BUILDQ REFUSED: numeric argument must be decimal digits: 12x" as *u8) * (rc12 == 4), ctr)
138 let rc13: i64 = qg_run(elf, "bogus" as *u8, "t1" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
139 gv_check("T11 an unknown verb prints usage (exit 2)" as *u8, gk_out_has(out, ol[0], "usage: nx_buildq add" as *u8) * (rc13 == 2), ctr)
140
141 let rc: i64 = gv_verdict("NX-BUILDQ-GATE" as *u8, ctr, "DL9 bq_dedupe_target: one row per target by id and the receipt says so; close marks a queued row closed in place with its reason; fired, closed and absent rows are named, never invented" as *u8)
142 sys_exit(rc)
143 return rc
144}