code wiki / _hdl_build / nx_gateorder_lib.nx
nx_gateorder_lib.nx source
↩ module page · 289 lines · 10032 B
1// nx_gateorder_lib.nx -- pure classifier for GATE VERDICT-EMIT INTEGRITY.
2//
3// WHY (found 2026-07-31 in my OWN gate, then swept): a gate that writes its verdict BEFORE its last
4// tooth runs is a FALSE-GREEN GENERATOR. ctr is read early, so a FAILING late tooth is still recorded
5// GREEN -- and the rollup / honesty gate / evidence audit read the LOG, not stdout, so the regression is
6// invisible to every ruler while the gate screams RED to a terminal nobody watches. It is the inverse of
7// "a guard placed after the act it guards": A RECORD WRITTEN BEFORE THE WORK IT RECORDS.
8//
9// The sweep that found this was a shell script in /tmp. A finding that is not wired into a standing
10// instrument decays -- which is this session's own repeated lesson -- so the classifier lives here as a
11// PURE function over a file buffer, gate-reachable, and the walker is a separate organ.
12//
13// ⚠CLASSIFY, NEVER ASSUME. UNKNOWN is a first-class verdict: my first detector missed a gate whose tooth
14// helper was named `ceg_t` and flagged a CLEAN gate as defective. AN AUDIT TOOL WITH AN INCOMPLETE
15// PATTERN ACCUSES THE INNOCENT, so a file whose teeth cannot be located is reported UNKNOWN, never GUILTY.
16// license_tier: ORIGINAL No hw writes (Rule 26).
17import "nx_syscalls.nx"
18
19const GO_NOEMIT: i64 = 0
20const GO_EARLY: i64 = 1
21const GO_LATE: i64 = 2
22const GO_UNKNOWN: i64 = 3
23
24func go_strlen(s: *u8) -> i64 {
25 var n: i64 = 0
26 while s[n] != (0 as u8) { n = n + 1 }
27 return n
28}
29
30func go_puts(s: *u8) {
31 sys_write(1, s, go_strlen(s))
32}
33
34func go_puti(x: i64) {
35 let b: *u8 = sys_mmap(64) as *u8
36 var v: i64 = x
37 var neg: i64 = 0
38 if v < 0 {
39 neg = 1
40 v = 0 - v
41 }
42 var i: i64 = 40
43 if v == 0 {
44 i = i - 1
45 b[i] = 48 as u8
46 }
47 while v > 0 {
48 let d: i64 = v - (v / 10) * 10
49 i = i - 1
50 b[i] = (d + 48) as u8
51 v = v / 10
52 }
53 if neg == 1 {
54 i = i - 1
55 b[i] = 45 as u8
56 }
57 sys_write(1, ((b as i64) + i) as *u8, 40 - i)
58}
59
60func go_kv(k: *u8, x: i64) {
61 go_puts(k)
62 go_puts("=" as *u8)
63 go_puti(x)
64 go_puts(" " as *u8)
65}
66
67func go_match_at(buf: *u8, at: i64, n: i64, pat: *u8) -> i64 {
68 let pl: i64 = go_strlen(pat)
69 if at + pl > n { return 0 }
70 var k: i64 = 0
71 var ok: i64 = 1
72 while k < pl {
73 if buf[at + k] != pat[k] { ok = 0 }
74 k = k + 1
75 }
76 return ok
77}
78
79// First byte offset of `pat`, or -1.
80func go_first(buf: *u8, n: i64, pat: *u8) -> i64 {
81 var i: i64 = 0
82 var hit: i64 = 0 - 1
83 while i < n {
84 if go_match_at(buf, i, n, pat) == 1 {
85 hit = i
86 i = n
87 } else { i = i + 1 }
88 }
89 return hit
90}
91
92// A tooth call is conventionally `<helper>("T<digit>` -- the label, not the helper name, because helper
93// names differ per gate (cg_t / cgg_t / ceg_t / cdg_t ...) and matching on them is how I accused an
94// innocent gate. Returns the LAST such offset, or -1.
95func go_last_tooth(buf: *u8, n: i64) -> i64 {
96 var i: i64 = 0
97 var hit: i64 = 0 - 1
98 while i < n {
99 if go_match_at(buf, i, n, "(\"T" as *u8) == 1 {
100 let c: i64 = buf[i + 3] as i64
101 if c >= 48 {
102 if c <= 57 { hit = i }
103 }
104 }
105 i = i + 1
106 }
107 return hit
108}
109
110// The verdict emit: either a *_gate_log helper call or a raw append-open of a log file.
111func go_emit_off(buf: *u8, n: i64) -> i64 {
112 let a: i64 = go_first(buf, n, "gate_log" as *u8)
113 let b: i64 = go_first(buf, n, "openat_append" as *u8)
114 if a < 0 { return b }
115 if b < 0 { return a }
116 if a < b { return a }
117 return b
118}
119
120// ⚠⚠THE ORDERING VERDICT WAS REMOVED, DELIBERATELY. An earlier version returned EMIT-EARLY by comparing
121// the first emit-token offset against the last tooth offset. It was WRONG ON ALL SEVEN gates it flagged,
122// and I had already filed a sev7 debt against other lanes before checking: the token's first textual
123// occurrence lands on HELPER FUNCTION DEFINITIONS (`func g_log`, `func cg_signal` -- a definition site
124// says nothing about when the call happens), on TEST FIXTURES, and on /tmp scratch writes. This gate's
125// own source self-flagged, because the pattern appears inside its TEST-DATA STRING LITERALS.
126//
127// ★★★★★TEXTUAL POSITION CANNOT DECIDE EXECUTION ORDER. The one genuine instance in this arc was found by
128// READING THE LOG (gate printed 41/41 while the log said 23/23) -- behavioural evidence. Grep never
129// would have found it, and grep DID manufacture seven accusations.
130//
131// So this classifier now answers only the question it can actually answer: DOES A DURABLE VERDICT EMIT
132// EXIST AT ALL? A WRONG ANSWER IS WORSE THAN AN ABSENT ONE, and a sev7 built on a bad detector is worse
133// than no sweep, because it sends other seats to edit correct code.
134func go_classify(buf: *u8, n: i64) -> i64 {
135 let e: i64 = go_emit_off(buf, n)
136 if e < 0 { return GO_NOEMIT }
137 return GO_LATE
138}
139
140func go_class_name(c: i64) -> *u8 {
141 if c == GO_NOEMIT { return "NO-EMIT" as *u8 }
142 if c == GO_LATE { return "emit-present(order NOT judged)" as *u8 }
143 return "UNKNOWN" as *u8
144}
145
146// Durable verdict emit. MUST be a gate's LAST act -- teeth added later would otherwise run after the
147// counters are read, and the record would disagree with the run. nx_gateverify exists to catch exactly
148// that, and it caught THIS gate having no record at all the first time it was pointed here.
149func go_gate_log(path: *u8, tag: *u8, passed: i64, total: i64) {
150 let fd: i64 = sys_openat_append(path, 0x1A4)
151 if fd < 0 { return }
152 sys_write(fd, tag, go_strlen(tag))
153 if passed == total { sys_write(fd, " verdict=GREEN passed=" as *u8, 22) }
154 else { sys_write(fd, " verdict=RED passed=" as *u8, 20) }
155 let b: *u8 = sys_mmap(128) as *u8
156 var v: i64 = passed
157 var i: i64 = 40
158 if v == 0 {
159 i = i - 1
160 b[i] = 48 as u8
161 }
162 while v > 0 {
163 let d: i64 = v - (v / 10) * 10
164 i = i - 1
165 b[i] = (d + 48) as u8
166 v = v / 10
167 }
168 sys_write(fd, ((b as i64) + i) as *u8, 40 - i)
169 sys_write(fd, " total=" as *u8, 7)
170 let b2: *u8 = sys_mmap(128) as *u8
171 var v2: i64 = total
172 var j: i64 = 40
173 if v2 == 0 {
174 j = j - 1
175 b2[j] = 48 as u8
176 }
177 while v2 > 0 {
178 let d2: i64 = v2 - (v2 / 10) * 10
179 j = j - 1
180 b2[j] = (d2 + 48) as u8
181 v2 = v2 / 10
182 }
183 sys_write(fd, ((b2 as i64) + j) as *u8, 40 - j)
184 sys_write(fd, "\n" as *u8, 1)
185 sys_close(fd)
186}
187
188// ---- BEHAVIOURAL VERIFICATION PARSERS ----
189// The textual ordering check was deleted because position cannot decide execution order. What CAN decide
190// it is running the gate and comparing what it PRINTS against what it RECORDED -- which is exactly how
191// the one real defect in this arc was caught (41/41 on stdout, 23/23 in the log). These parsers are the
192// pure half of that comparison, so they are gate-reachable.
193
194// Parse the LAST "<a>/<b>" ratio in a buffer (a gate footer prints "NAME 41/41 GREEN"). out[0]=a out[1]=b.
195// Returns 1 on success, 0 if no ratio found. Takes the LAST because banner text may contain earlier ones.
196func go_is_digit(c: i64) -> i64 {
197 if c < 48 { return 0 }
198 if c > 57 { return 0 }
199 return 1
200}
201
202func go_last_ratio(buf: *u8, n: i64, out: *i64) -> i64 {
203 var found: i64 = 0
204 var i: i64 = 0
205 while i < n {
206 if buf[i] == (47 as u8) {
207 // walk LEFT over digits (flag loop -- never assign the loop var as a pseudo-break)
208 var lstart: i64 = i
209 var scanning: i64 = 1
210 while scanning == 1 {
211 if lstart <= 0 { scanning = 0 }
212 else {
213 if go_is_digit(buf[lstart - 1] as i64) == 1 { lstart = lstart - 1 }
214 else { scanning = 0 }
215 }
216 }
217 // walk RIGHT over digits
218 var rend: i64 = i + 1
219 var scanning2: i64 = 1
220 while scanning2 == 1 {
221 if rend >= n { scanning2 = 0 }
222 else {
223 if go_is_digit(buf[rend] as i64) == 1 { rend = rend + 1 }
224 else { scanning2 = 0 }
225 }
226 }
227 if lstart < i {
228 if rend > i + 1 {
229 var a: i64 = 0
230 var p: i64 = lstart
231 while p < i {
232 a = a * 10 + (buf[p] as i64 - 48)
233 p = p + 1
234 }
235 var b: i64 = 0
236 var q: i64 = i + 1
237 while q < rend {
238 b = b * 10 + (buf[q] as i64 - 48)
239 q = q + 1
240 }
241 out[0] = a
242 out[1] = b
243 found = 1
244 }
245 }
246 }
247 i = i + 1
248 }
249 return found
250}
251
252// Value of "<key>=<digits>" (last occurrence), or -1 when the key is absent.
253func go_kv_num(buf: *u8, n: i64, key: *u8) -> i64 {
254 let kl: i64 = go_strlen(key)
255 var val: i64 = 0 - 1
256 var i: i64 = 0
257 while i < n {
258 if go_match_at(buf, i, n, key) == 1 {
259 var v: i64 = 0
260 var any: i64 = 0
261 var j: i64 = i + kl
262 var stop: i64 = 0
263 while stop == 0 {
264 if j >= n { stop = 1 }
265 else {
266 let c: i64 = buf[j] as i64
267 if c < 48 { stop = 1 }
268 else {
269 if c > 57 { stop = 1 }
270 else {
271 v = v * 10 + (c - 48)
272 any = 1
273 j = j + 1
274 }
275 }
276 }
277 }
278 if any == 1 { val = v }
279 }
280 i = i + 1
281 }
282 return val
283}
284
285func go_ends_gate_nx(name: *u8) -> i64 {
286 let n: i64 = go_strlen(name)
287 if n < 9 { return 0 }
288 return go_match_at(name, n - 8, n, "_gate.nx" as *u8)
289}