nx_barfresh_gate.nx source
↩ module page · 219 lines · 16866 B
1// nx_barfresh_gate.nx -- GATE for the bar-age ruler (nx_barfresh_lib), driven IN-PROCESS on planted plan rows with the
2// current month HANDED IN, so every state is reached deterministically: a fresh bar, an older bar nobody looked for this
3// month (the hard fail), an older bar attested this month as the newest the listing has, an older bar a this-month scan
4// contradicts, an attestation from LAST month (does not count), an attestation for a DIFFERENT listing (does not count),
5// a plan with no bars (NO-BAR, not refused), malformed and future months, a year wrap, NUL-terminated lines as the ranker
6// hands them over, a mixed board whose partition must sum, a declaration made twice (a repeated bar id, a repeated
7// same-day scan) refused with its rule named while two scans on different days stay two attestations, the month-of-epoch derivation against the estate's own civil
8// date at a month boundary, and the two-tree plan read on /tmp trees. Every fixture asserts its own condition first.
9// license_tier: ORIGINAL No hw writes (Rule 26).
10import "nx_syscalls.nx"
11import "nx_gatekit_lib.nx"
12import "nx_barfresh_lib.nx"
13import "nx_gate_verdict.nx"
14
15const G_ROOT: *u8 = "/tmp/nx_barfresh_gate"
16const G_TREE_A: *u8 = "/tmp/nx_barfresh_gate/a/"
17const G_TREE_B: *u8 = "/tmp/nx_barfresh_gate/b/"
18const G_TREE_A_DIR: *u8 = "/tmp/nx_barfresh_gate/a"
19const G_TREE_B_DIR: *u8 = "/tmp/nx_barfresh_gate/b"
20const G_PLAN_B: *u8 = "/tmp/nx_barfresh_gate/b/probe.plan"
21const G_DOM: *u8 = "probe"
22const G_DOM_ABSENT: *u8 = "nowhere"
23const G_I64: i64 = 8
24const G_MAX_BARS: i64 = 8
25// the current month handed in: September 2026, and January 2026 for the year wrap
26const G_CUR_Y: i64 = 2026
27const G_CUR_M: i64 = 9
28const G_WRAP_Y: i64 = 2026
29const G_WRAP_M: i64 = 1
30// epochs at a month boundary: 2026-08-31T00:00:00Z and 2026-09-01T00:00:00Z (days 20696 and 20697 since 1970-01-01)
31const G_EPOCH_AUG31: i64 = 1788134400
32const G_EPOCH_SEP01: i64 = 1788220800
33const G_DAY_2026_09_06: i64 = 20702
34const G_Y_1970: i64 = 1970
35// fixtures: every one carries a rung row too, the shape a real plan has around its bars
36const G_FRESH: *u8 = "rung|X1|a rung|sym|d|Organ|1|-\nsotabar|B1|2026-09|aspect extraction|what september says|k1|note\n"
37const G_UNATTESTED: *u8 = "rung|X1|a rung|sym|d|Organ|1|-\nsotabar|B1|2026-08|aspect extraction|what august says|k1|note\n"
38const G_ATTESTED: *u8 = "rung|X1|a rung|sym|d|Organ|1|-\nsotabar|B1|2026-08|aspect extraction|what august says|k1|note\nbarscan|2026-09-06|k1|newest=2026-08|the listing was read, newest entry august\n"
39const G_CONTRADICTED: *u8 = "sotabar|B1|2026-07|aspect extraction|what july says|k1|note\nbarscan|2026-09-06|k1|newest=2026-08|the listing has an august entry the bar ignores\n"
40const G_OLD_SCAN: *u8 = "sotabar|B1|2026-08|aspect extraction|what august says|k1|note\nbarscan|2026-08-30|k1|newest=2026-08|a scan from last month\n"
41const G_OTHER_REF: *u8 = "sotabar|B1|2026-08|aspect extraction|what august says|k1|note\nbarscan|2026-09-06|k2|newest=2026-08|a scan of a different listing\n"
42const G_NOBAR: *u8 = "rung|X1|a rung|sym|d|Organ|1|-\nlog|1|X1|measure|no bars declared\n"
43const G_MALFORMED: *u8 = "sotabar|B1|2026-13|subject|says|k1|thirteenth month\n"
44const G_FUTURE: *u8 = "sotabar|B1|2026-10|subject|says|k1|a month that has not happened\n"
45const G_WRAP: *u8 = "sotabar|B1|2025-12|subject|says|k1|note\nbarscan|2026-01-03|k1|newest=2025-12|december attested in january\n"
46const G_MIXED: *u8 = "sotabar|B1|2026-09|subject|says|k1|fresh\nsotabar|B2|2026-08|subject|says|k2|nobody looked\n"
47const G_MIXED_BARS: i64 = 2
48// a declaration made twice (2026-09-14: an intake landed twice across a transport drop; the rung id was refused by the
49// plan-graph rule and the doubled bar, scan and target were not)
50const G_DUPBAR: *u8 = "sotabar|B1|2026-09|subject|says|k1|declared once\nsotabar|B1|2026-09|subject|says again|k1|declared twice\n"
51const G_DUPBAR_BARS: i64 = 2
52const G_DUPSCAN: *u8 = "sotabar|B1|2026-08|subject|says|k1|note\nbarscan|2026-09-06|k1|newest=2026-08|read once\nbarscan|2026-09-06|k1|newest=2026-08|the same read landed twice\n"
53const G_DUPSCAN_SCANS: i64 = 2
54const G_TWO_SCANS: *u8 = "sotabar|B1|2026-08|subject|says|k1|note\nbarscan|2026-09-02|k1|newest=2026-08|read early in the month\nbarscan|2026-09-06|k1|newest=2026-08|read again on another day\n"
55const G_RULE_BAR_DUP: *u8 = "BAR-DUPLICATE"
56const G_RULE_SCAN_DUP: *u8 = "SCAN-DUPLICATE"
57const G_RULE_NONE: *u8 = "NONE"
58
59// two NUL-terminated strings byte-equal
60func g_str_is(a: *u8, b: *u8) -> i64 {
61 let n: i64 = bf_slen(a)
62 if n != bf_slen(b) { return 0 }
63 var i: i64 = 0
64 while i < n { if a[i] != b[i] { return 0 } i = i + 1 }
65 return 1
66}
67
68func g_copy(src: *u8) -> *u8 {
69 let n: i64 = bf_slen(src)
70 let d: *u8 = sys_mmap(n + 1)
71 var i: i64 = 0
72 while i < n { d[i] = src[i]; i = i + 1 }
73 d[n] = 0 as u8
74 return d
75}
76
77// classify a fixture string against a handed-in month; returns the number of bars, fills the census, and st0 = the
78// first bar's state (or BF_NONE when there is none)
79func g_run(fx: *u8, cur: i64, c: *i64, st0: *i64, seen0: *i64) -> i64 {
80 let off: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
81 let ym: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
82 let st: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
83 let seen: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
84 let nb: i64 = bf_classify(fx, bf_slen(fx), cur, off, ym, st, seen, c)
85 st0[0] = BF_NONE
86 seen0[0] = BF_NONE
87 if nb > 0 { st0[0] = st[0]; seen0[0] = seen[0] }
88 return nb
89}
90
91func main() -> i64 {
92 gv_head("=== nx_barfresh_gate -- a bar is the current month's, or attested this month as the newest, or it is refused ===" as *u8)
93 let c: *i64 = gv_ctr()
94 let cur: i64 = bf_ym_index(G_CUR_Y, G_CUR_M)
95 let cen: *i64 = sys_mmap(BF_C_N * G_I64) as *i64
96 let st0: *i64 = sys_mmap(G_I64) as *i64
97 let seen0: *i64 = sys_mmap(G_I64) as *i64
98 // month arithmetic and parsing
99 gv_check_eq("month-index-round-trips-year (2026-09)" as *u8, bf_ym_year(cur), G_CUR_Y, c)
100 gv_check_eq("month-index-round-trips-month (2026-09)" as *u8, bf_ym_month(cur), G_CUR_M, c)
101 gv_check_eq("parse-YYYY-MM" as *u8, bf_parse_ym("2026-09" as *u8, 7), cur, c)
102 gv_check_eq("parse-YYYY-MM-DD-keeps-the-month" as *u8, bf_parse_ym("2026-09-06" as *u8, 10), cur, c)
103 gv_check_eq("neg-control-parse-refuses-a-thirteenth-month" as *u8, bf_parse_ym("2026-13" as *u8, 7), BF_NONE, c)
104 gv_check_eq("neg-control-parse-refuses-a-short-token" as *u8, bf_parse_ym("2026-9" as *u8, 6), BF_NONE, c)
105 gv_check_eq("neg-control-parse-refuses-a-trailing-non-dash" as *u8, bf_parse_ym("2026-09x" as *u8, 8), BF_NONE, c)
106 gv_check_eq("parse-newest-tag" as *u8, bf_parse_newest("newest=2026-08" as *u8, 14), bf_ym_index(G_CUR_Y, G_CUR_M - 1), c)
107 gv_check_eq("neg-control-parse-newest-refuses-a-bare-month" as *u8, bf_parse_newest("2026-08" as *u8, 7), BF_NONE, c)
108 // the month of an epoch, through the estate's civil date, at a month boundary
109 gv_check_eq("month-of-epoch: 2026-08-31T00:00Z is august" as *u8, bf_ym_of_epoch(G_EPOCH_AUG31), bf_ym_index(G_CUR_Y, G_CUR_M - 1), c)
110 gv_check_eq("month-of-epoch: 2026-09-01T00:00Z is september" as *u8, bf_ym_of_epoch(G_EPOCH_SEP01), cur, c)
111 gv_check_eq("month-of-epoch: the epoch itself is 1970-01" as *u8, bf_ym_of_epoch(0), bf_ym_index(G_Y_1970, 1), c)
112 gv_check_eq("civil-date-incumbent-agrees: days_from_civil(2026-09-06) is day 20702" as *u8, days_from_civil(G_CUR_Y, G_CUR_M, 6), G_DAY_2026_09_06, c)
113 gv_check("the-clock-reads-a-month-no-earlier-than-the-fixture-month (the box's clock is not in the past)" as *u8, (bf_now_ym() >= cur) as i64, c)
114 // the states
115 var nb: i64 = g_run(G_FRESH, cur, cen, st0, seen0)
116 gv_check_eq("fixture-reached-the-condition: the fresh plan carries one bar" as *u8, nb, 1, c)
117 gv_check_eq("a-bar-dated-the-current-month-is-FRESH" as *u8, st0[0], BF_S_FRESH, c)
118 gv_check_eq("a-fresh-board-verdict-is-FRESH (exit 0)" as *u8, bf_verdict(cen), BF_EXIT_FRESH, c)
119 nb = g_run(G_UNATTESTED, cur, cen, st0, seen0)
120 gv_check_eq("neg-control-an-older-bar-nobody-looked-for-this-month-is-UNATTESTED" as *u8, st0[0], BF_S_UNATTESTED, c)
121 gv_check_eq("neg-control-and-the-verdict-is-the-hard-fail (exit 1)" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
122 nb = g_run(G_ATTESTED, cur, cen, st0, seen0)
123 gv_check_eq("fixture-reached-the-condition: the attested plan carries one this-month scan" as *u8, cen[BF_C_SCANS_CUR], 1, c)
124 gv_check_eq("an-older-bar-attested-this-month-as-the-newest-is-ATTESTED" as *u8, st0[0], BF_S_ATTESTED, c)
125 gv_check_eq("the-attestation-names-the-month-seen (august)" as *u8, seen0[0], bf_ym_index(G_CUR_Y, G_CUR_M - 1), c)
126 gv_check_eq("an-attested-board-verdict-is-FRESH (exit 0): the fallback month is named, not hidden" as *u8, bf_verdict(cen), BF_EXIT_FRESH, c)
127 nb = g_run(G_CONTRADICTED, cur, cen, st0, seen0)
128 gv_check_eq("neg-control-a-bar-older-than-what-this-month's-scan-saw-is-STALE" as *u8, st0[0], BF_S_STALE, c)
129 gv_check_eq("neg-control-and-refused (exit 1)" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
130 nb = g_run(G_OLD_SCAN, cur, cen, st0, seen0)
131 gv_check_eq("fixture-reached-the-condition: the scan is from last month (this-month scans 0)" as *u8, cen[BF_C_SCANS_CUR], 0, c)
132 gv_check_eq("neg-control-last-month's-attestation-does-not-count: UNATTESTED" as *u8, st0[0], BF_S_UNATTESTED, c)
133 nb = g_run(G_OTHER_REF, cur, cen, st0, seen0)
134 gv_check_eq("neg-control-an-attestation-for-a-different-listing-does-not-count: UNATTESTED" as *u8, st0[0], BF_S_UNATTESTED, c)
135 nb = g_run(G_NOBAR, cur, cen, st0, seen0)
136 gv_check_eq("a-plan-with-no-bar-rows-counts-zero-bars" as *u8, nb, 0, c)
137 gv_check_eq("no-bars-is-NO-BAR (exit 3), neither acquitted nor refused" as *u8, bf_verdict(cen), BF_EXIT_NOBAR, c)
138 nb = g_run(G_MALFORMED, cur, cen, st0, seen0)
139 gv_check_eq("neg-control-a-month-that-does-not-parse-is-MALFORMED" as *u8, st0[0], BF_S_MALFORMED, c)
140 gv_check_eq("neg-control-malformed-is-refused (exit 1)" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
141 nb = g_run(G_FUTURE, cur, cen, st0, seen0)
142 gv_check_eq("neg-control-a-month-after-the-current-one-is-MALFORMED" as *u8, st0[0], BF_S_MALFORMED, c)
143 nb = g_run(G_WRAP, bf_ym_index(G_WRAP_Y, G_WRAP_M), cen, st0, seen0)
144 gv_check_eq("year-wrap: december attested in january is ATTESTED" as *u8, st0[0], BF_S_ATTESTED, c)
145 // NUL-terminated lines, as the ranker leaves its buffer after parsing rungs in place
146 let nulled: *u8 = g_copy(G_ATTESTED)
147 let nn: i64 = bf_slen(G_ATTESTED)
148 var q: i64 = 0
149 var nuls: i64 = 0
150 while q < nn { if nulled[q] == (BF_NL as u8) { nulled[q] = 0 as u8; nuls = nuls + 1 } q = q + 1 }
151 gv_check_eq("fixture-reached-the-condition: three line ends replaced by NULs" as *u8, nuls, 3, c)
152 let off2: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
153 let ym2: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
154 let st2: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
155 let seen2: *i64 = sys_mmap(G_MAX_BARS * G_I64) as *i64
156 let nb2: i64 = bf_classify(nulled, nn, cur, off2, ym2, st2, seen2, cen)
157 gv_check_eq("NUL-terminated-lines-find-the-bar" as *u8, nb2, 1, c)
158 gv_check_eq("NUL-terminated-lines-classify-identically (ATTESTED)" as *u8, st2[0], BF_S_ATTESTED, c)
159 // a mixed board: the partition sums and the worst bar decides
160 nb = g_run(G_MIXED, cur, cen, st0, seen0)
161 gv_check_eq("mixed-board-counts-both-bars" as *u8, nb, G_MIXED_BARS, c)
162 gv_check_eq("mixed-board-fresh-count" as *u8, cen[BF_C_FRESH], 1, c)
163 gv_check_eq("mixed-board-unattested-count" as *u8, cen[BF_C_UNATTESTED], 1, c)
164 gv_check_eq("mixed-board-partition-sums-to-bars" as *u8, bf_partition_sum(cen), cen[BF_C_BARS], c)
165 gv_check_eq("mixed-board-the-worst-bar-decides: refused" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
166 // banked here because the census array is reused by the tree-read teeth below: an emitted value must be the value
167 // its name promises, not whatever the last fixture left behind
168 let mixed_bars: i64 = nb
169 let mixed_sum: i64 = bf_partition_sum(cen)
170 // a declaration made twice: a repeated bar id, a repeated same-day scan; and the control that two scans of one
171 // listing on different days are two attestations
172 nb = g_run(G_DUPBAR, cur, cen, st0, seen0)
173 gv_check_eq("fixture-reached-the-condition: two sotabar rows carry one id" as *u8, nb, G_DUPBAR_BARS, c)
174 gv_check_eq("the-first-row-with-an-id-keeps-its-own-state (FRESH)" as *u8, st0[0], BF_S_FRESH, c)
175 gv_check_eq("neg-control-the-later-row-with-the-same-id-is-DUPLICATE (duplicate 1)" as *u8, cen[BF_C_DUPLICATE], 1, c)
176 gv_check_eq("neg-control-a-duplicate-bar-is-refused (exit 1) even though both rows are the current month" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
177 gv_check_eq("a-duplicate-bar-keeps-the-partition-summing-to-bars" as *u8, bf_partition_sum(cen), cen[BF_C_BARS], c)
178 gv_check("the-refusal-names-its-rule (BAR-DUPLICATE)" as *u8, g_str_is(bf_refusal_rule(cen), G_RULE_BAR_DUP), c)
179 let dup_bars: i64 = cen[BF_C_DUPLICATE]
180 nb = g_run(G_DUPSCAN, cur, cen, st0, seen0)
181 gv_check_eq("fixture-reached-the-condition: two scans dated the same day for one ref" as *u8, cen[BF_C_SCANS], G_DUPSCAN_SCANS, c)
182 gv_check_eq("neg-control-a-repeated-barscan-same-date-same-ref-is-a-duplicate-scan (scans_dup 1)" as *u8, cen[BF_C_SCANS_DUP], 1, c)
183 gv_check_eq("the-bar-itself-still-reads-ATTESTED" as *u8, st0[0], BF_S_ATTESTED, c)
184 gv_check_eq("neg-control-a-duplicate-scan-is-refused-even-though-the-bar-is-attested (exit 1)" as *u8, bf_verdict(cen), BF_EXIT_STALE, c)
185 gv_check("the-refusal-names-its-rule (SCAN-DUPLICATE)" as *u8, g_str_is(bf_refusal_rule(cen), G_RULE_SCAN_DUP), c)
186 nb = g_run(G_TWO_SCANS, cur, cen, st0, seen0)
187 gv_check_eq("two-scans-of-one-listing-on-different-days-are-two-attestations (scans_dup 0)" as *u8, cen[BF_C_SCANS_DUP], 0, c)
188 gv_check_eq("and-that-board-is-FRESH (exit 0)" as *u8, bf_verdict(cen), BF_EXIT_FRESH, c)
189 gv_check("a-clean-board-has-no-refusal-rule (NONE)" as *u8, g_str_is(bf_refusal_rule(cen), G_RULE_NONE), c)
190 // the two-tree read on /tmp trees: only tree B holds the plan
191 gk_mkdir(G_ROOT)
192 gk_mkdir(G_TREE_A_DIR)
193 gk_mkdir(G_TREE_B_DIR)
194 gk_write(G_PLAN_B, G_ATTESTED)
195 gv_check_eq("fixture-reached-the-condition: the plan was written into tree B only" as *u8, gk_exists(G_PLAN_B), 1, c)
196 let lenp: *i64 = sys_mmap(G_I64) as *i64
197 let which: *i64 = sys_mmap(G_I64) as *i64
198 lenp[0] = 0
199 which[0] = BF_NONE
200 let rb: *u8 = bf_read_plan(G_TREE_A, G_TREE_B, G_DOM, lenp, which)
201 gv_check_eq("two-tree-read-falls-through-to-tree-B (which = SECONDARY position)" as *u8, which[0], CT_TREE_SECONDARY, c)
202 gv_check_eq("two-tree-read-returns-the-whole-plan (bytes)" as *u8, lenp[0], bf_slen(G_ATTESTED), c)
203 let nbr: i64 = bf_classify(rb, lenp[0], cur, off2, ym2, st2, seen2, cen)
204 gv_check_eq("the-bytes-read-classify-as-planted (ATTESTED)" as *u8, st2[0], BF_S_ATTESTED, c)
205 lenp[0] = 0
206 bf_read_plan(G_TREE_A, G_TREE_B, G_DOM_ABSENT, lenp, which)
207 gv_check_eq("neg-control-a-domain-in-neither-tree-reads-nothing" as *u8, which[0], CT_TREE_NONE, c)
208 gv_check("neg-control-a-domain-in-neither-tree-has-no-bytes" as *u8, (lenp[0] <= 0) as i64, c)
209 gv_values_head()
210 gv_kv("cur_month_index" as *u8, cur)
211 gv_kv("epoch_aug31_month" as *u8, bf_ym_of_epoch(G_EPOCH_AUG31))
212 gv_kv("epoch_sep01_month" as *u8, bf_ym_of_epoch(G_EPOCH_SEP01))
213 gv_kv("mixed_bars" as *u8, mixed_bars)
214 gv_kv("mixed_partition_sum" as *u8, mixed_sum)
215 gv_kv("nul_lines_bars" as *u8, nb2)
216 gv_kv("tree_b_plan_bytes" as *u8, bf_slen(G_ATTESTED))
217 gv_kv("dupbar_duplicate_count" as *u8, dup_bars)
218 return gv_verdict("nx_barfresh_gate" as *u8, c, "the bar-age ruler proven on planted plan rows with the current month handed in: a bar dated the current month is FRESH, an older bar nobody looked for this month is the hard fail, an older bar attested this month as the newest the listing has passes with its month named, a bar a this-month scan contradicts is STALE, last-month and other-listing attestations do not count, a plan with no bars is NO-BAR rather than refused or acquitted, malformed and future months are refused, the year wraps, NUL-terminated lines classify as newline-terminated ones, a mixed board sums its partition and is decided by its worst bar, a repeated bar id or a repeated same-day scan is refused with its rule named while two scans on different days stay two attestations, the month of an epoch agrees with the estate's civil date at a month boundary, and the plan read falls through the two trees and says which answered; every fixture asserts its own condition first" as *u8)
219}