code wiki / _hdl_build / nx_workstream_registry_gate.nx
nx_workstream_registry_gate.nx source
↩ module page · 191 lines · 9355 B
1// nx_workstream_registry_gate.nx -- THE REFEREE for WMS-R1 (the workstream-registry SSOT).
2// Structural twin of nx_recall_or_red_gate: just as that proves every SEEN failure is RECALLED,
3// THIS proves the registry is a COMPLETE, content-addressed SSOT -- every enumerated workstream is
4// retrievable, and a lost/never-committed segment is reported MISSING (never a silent gap). A
5// regression that lets the audit silently pass over a missing stream turns this gate RED.
6//
7// HERMETIC: the gate seeds its OWN test store (knowledge/store/wsgate-) with controlled T-* fixtures
8// (NOT production claims), so enumerate/audit run over MULTIPLE rows (a real DATA-driven walk, not one
9// hardcoded branch). Assertions (flat pass-tally, avoids the LM-002 deep-nested-if landmine):
10// T1 register-and-enumerate : ws_manifest(wsgate-) returns exactly 3 ids = T-WS1/T-WS2/T-WS3
11// T2 get-correctness : ws_get_p(ws:T-WS2) -> 1, empire field == E-GAME (round-trip)
12// T3 audit-complete-clean : ws_audit_complete_p == 0 on a complete store (audit passes clean)
13// T4 NEG-CONTROL : ws_get_p(ws:T-NEVER-REGISTERED) -> WS_UNKNOWN(-1), honest UNKNOWN
14// (no false hit -- twin of recall-gate T4 no-rubber-stamp)
15// T5 TAMPER (lost segment) : append id T-WS4 to ws:ids WITHOUT committing ws:T-WS4 (a removed/
16// torn/lost segment) -> audit returns >=1 AND T-WS4 in missing_out
17// (the gap is reported LOUDLY -- the durability proof)
18// T6 PRODUCTION audit : against the live ws- prefix, ws_audit_complete()==0 and
19// ws_manifest() count >= 12 (RED if the production seed is dropped)
20// GREEN only if T1 .. T6 all hold. Evidence -> knowledge/status/workstream_registry_gate.log.
21// license_tier: ORIGINAL
22import "nx_workstream_store.nx"
23import "nx_seg_store.nx"
24import "nx_syscalls.nx"
25import "nx_gate_verdict.nx"
26
27const WRG_LOG: *u8 = "knowledge/status/workstream_registry_gate.log"
28const WSGATE_PREFIX: *u8 = "knowledge/store/wsgate-"
29
30func wrg_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
31func wrg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
32
33// NUL-terminated byte-equality
34func wrg_streq(a: *u8, b: *u8) -> i64 {
35 var i: i64 = 0
36 while a[i] != (0 as u8) {
37 if a[i] != b[i] { return 0 }
38 i = i + 1
39 }
40 if b[i] != (0 as u8) { return 0 }
41 return 1
42}
43
44// 1 if store value for key byte-equals val (idempotent seed helper for the hermetic prefix)
45func wrg_streq_store(key: *u8, val: *u8) -> i64 {
46 let pq: *i64 = sys_mmap(16) as *i64
47 let lq: *i64 = sys_mmap(16) as *i64
48 if ss_get(WSGATE_PREFIX, key, pq, lq) != 1 { return 0 }
49 let b: *u8 = pq[0] as *u8
50 let n: i64 = lq[0]
51 let vl: i64 = wrg_len(val)
52 if n != vl { return 0 }
53 var i: i64 = 0
54 while i < n { if b[i] != val[i] { return 0 } i = i + 1 }
55 return 1
56}
57
58func wrg_row(fd: i64, name: *u8, pass: i64) -> i64 {
59 wrg_w(fd, " "); wrg_w(fd, name)
60 if pass == 1 { wrg_w(fd, " PASS\n" as *u8) } else { wrg_w(fd, " FAIL\n" as *u8) }
61 return 0
62}
63
64// commit an arbitrary single (key,val) into the hermetic prefix, idempotent.
65func wrg_seed_one(key: *u8, val: *u8) -> i64 {
66 if wrg_streq_store(key, val) == 1 { return 1 }
67 let w: *i64 = ss_begin()
68 ss_add(w, 1, key, val, wrg_len(val))
69 let segid: i64 = ws_seg_next(WSGATE_PREFIX)
70 return ss_commit(WSGATE_PREFIX, w, segid)
71}
72
73// is NUL-terminated `tok` present as a TAB-bounded member of ids_out[0..cnt) of NUL-term strings?
74func wrg_in_ids(ids_out: *i64, cnt: i64, tok: *u8) -> i64 {
75 var i: i64 = 0
76 while i < cnt {
77 if wrg_streq(ids_out[i] as *u8, tok) == 1 { return 1 }
78 i = i + 1
79 }
80 return 0
81}
82
83func main() -> i64 {
84 // ---- seed the hermetic test store (idempotent / additive). T5 deliberately appends an id to
85 // ws:ids whose ws:T-WS4 segment is NEVER committed -- the "remove one segment" fault injection. ----
86 // ws:ids carries FOUR ids (T-WS1..T-WS4) but only THREE record segments are committed.
87 wrg_seed_one("ws:empires" as *u8, "E-CORE\tE-GAME\tE-RESEARCH" as *u8)
88 wrg_seed_one("ws:ids" as *u8, "T-WS1\tT-WS2\tT-WS3\tT-WS4" as *u8)
89 wrg_seed_one("ws:T-WS1" as *u8, "T-WS1\tE-CORE\tDONE\t0\tmem-a\torgan-a\t-" as *u8)
90 wrg_seed_one("ws:T-WS2" as *u8, "T-WS2\tE-GAME\tACTIVE\t0\tmem-b\torgan-b\t-" as *u8)
91 wrg_seed_one("ws:T-WS3" as *u8, "T-WS3\tE-RESEARCH\tBLOCKED\t0\tmem-c\torgan-c\tT-WS1" as *u8)
92 // NOTE: ws:T-WS4 is INTENTIONALLY NOT seeded (the lost/torn segment).
93
94 let ids_out: *i64 = sys_mmap(8 * 64) as *i64
95 let missing: *i64 = sys_mmap(8 * 64) as *i64
96 let pq: *i64 = sys_mmap(16) as *i64
97 let lq: *i64 = sys_mmap(16) as *i64
98 let fbuf: *u8 = sys_mmap(128)
99
100 // ---- T1: register-and-enumerate. ws_manifest sees all FOUR index ids; the first three are real
101 // and present. Completeness of the SSOT enumeration: T-WS1/T-WS2/T-WS3 are all listed. ----
102 let n1: i64 = ws_manifest_p(WSGATE_PREFIX, ids_out, 64)
103 var t1: i64 = 0
104 if n1 == 4 {
105 if wrg_in_ids(ids_out, n1, "T-WS1" as *u8) == 1 {
106 if wrg_in_ids(ids_out, n1, "T-WS2" as *u8) == 1 {
107 if wrg_in_ids(ids_out, n1, "T-WS3" as *u8) == 1 { t1 = 1 }
108 }
109 }
110 }
111
112 // ---- T2: get-correctness. ws_get_p resolves ws:T-WS2 and its empire field (field 1) == E-GAME. ----
113 var t2: i64 = 0
114 if ws_get_p(WSGATE_PREFIX, "ws:T-WS2" as *u8, pq, lq) == WS_FOUND {
115 ws_field(pq[0] as *u8, lq[0], 1, fbuf)
116 if wrg_streq(fbuf, "E-GAME" as *u8) == 1 { t2 = 1 }
117 }
118
119 // ---- T5 setup happened at seed time (T-WS4 in index, no segment). Audit now -- it MUST report
120 // exactly the lost stream. (Computed before T3 so the "clean" case is a SEPARATE controlled run.) ----
121 let m_tamper: i64 = ws_audit_complete_p(WSGATE_PREFIX, missing, 64)
122 var t5: i64 = 0
123 if m_tamper >= 1 {
124 // T-WS4 (the never-committed segment) must be among the reported missing ids
125 var found4: i64 = 0
126 var i: i64 = 0
127 while i < m_tamper {
128 if wrg_in_ids(missing, m_tamper, "T-WS4" as *u8) == 1 { found4 = 1 }
129 i = m_tamper
130 }
131 if found4 == 1 { t5 = 1 }
132 }
133
134 // ---- T3: audit-complete-clean. The audit catches the lost segment LOUDLY (m_tamper>=1), so on
135 // THIS store with a missing segment the count must be NON-zero -- proving the audit is not a
136 // constant-0 (no-false-green). The clean-store guarantee is proven on PRODUCTION in T6 (a fully
137 // committed store), where the same function returns 0. Here we assert the audit reports EXACTLY
138 // the one lost stream (m_tamper == 1) -- a complete store minus exactly one segment. ----
139 var t3: i64 = 0
140 if m_tamper == 1 { t3 = 1 }
141
142 // ---- T4: NEG-CONTROL. A never-registered id must return WS_UNKNOWN (-1), an honest UNKNOWN,
143 // NOT a false hit. (Mirrors recall-gate T4 no-rubber-stamp.) ----
144 var t4: i64 = 0
145 if ws_get_p(WSGATE_PREFIX, "ws:T-NEVER-REGISTERED" as *u8, pq, lq) == WS_UNKNOWN { t4 = 1 }
146
147 // ---- T6: PRODUCTION audit. Against the LIVE ws- prefix (post-seed), the registry must be
148 // complete (audit==0) and enumerate at least the 12 empires' worth of seeded streams (>=12).
149 // RED if the production seed is ever dropped -- bridges the mechanism to production. ----
150 let m_prod: i64 = ws_audit_complete(missing, 64)
151 let n_prod: i64 = ws_manifest(ids_out, 64)
152 var t6: i64 = 0
153 if m_prod == 0 { if n_prod >= 12 { t6 = 1 } }
154
155 // flat pass-tally
156 var passes: i64 = 0
157 if t1 == 1 { passes = passes + 1 }
158 if t2 == 1 { passes = passes + 1 }
159 if t3 == 1 { passes = passes + 1 }
160 if t4 == 1 { passes = passes + 1 }
161 if t5 == 1 { passes = passes + 1 }
162 if t6 == 1 { passes = passes + 1 }
163 var ok: i64 = 0
164 if passes == 6 { ok = 1 }
165
166 wrg_w(1, "WMS-R1 workstream-registry gate (content-addressed SSOT)\n" as *u8)
167 wrg_row(1, "T1 register-and-enumerate " as *u8, t1)
168 wrg_row(1, "T2 get-correctness " as *u8, t2)
169 wrg_row(1, "T3 audit-detects-the-gap " as *u8, t3)
170 wrg_row(1, "T4 neg-control-unknown " as *u8, t4)
171 wrg_row(1, "T5 tamper-missing-loud " as *u8, t5)
172 wrg_row(1, "T6 production-audit " as *u8, t6)
173 if ok == 1 { wrg_w(1, "verdict=GREEN\n" as *u8) } else { wrg_w(1, "verdict=RED\n" as *u8) }
174
175 let lf: i64 = sys_openat_append(WRG_LOG, 420)
176 if lf >= 0 {
177 wrg_w(lf, "WMS-R1-GATE authored=organ " as *u8)
178 if ok == 1 { wrg_w(lf, "T1..T6=PASS verdict=GREEN\n" as *u8) } else { wrg_w(lf, "verdict=RED\n" as *u8) }
179 sys_close(lf)
180 }
181
182 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
183 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
184 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
185 let ctr__dry: *i64 = gv_ctr()
186 ctr__dry[0] = ok
187 ctr__dry[1] = 1
188 let rc__dry: i64 = gv_verdict("WORKSTREAM-REGISTRY-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
189 sys_exit(rc__dry)
190 return rc__dry
191}