nx_seg_store_gate.nx source
↩ module page · 187 lines · 10828 B
1// nx_seg_store_gate.nx -- gates the sovereign append-only store: put a value, get it back byte-exact, absent key -> not
2// found. Turns the seg_store axis from LIVE-proven to GATE-proven. license_tier: ORIGINAL
3import "nx_seg_store.nx"
4import "nx_gate.nx"
5import "nx_gate_verdict.nx"
6
7func main() -> i64 {
8 gw("=== nx_seg_store_gate: append-only store put/get round-trip ===\n" as *u8)
9 var pass: i64=0; var tot: i64=0
10 let P: *u8 = "knowledge/ssgate-test" as *u8
11
12 // canonical writer idiom: fresh max+1 segid (uncapped); the old count-as-segid clobbers past the cap
13 let segid: i64 = ss_next_segid(P)
14 let w: *i64 = ss_begin()
15 ss_add(w, 1, "k1" as *u8, "hello-seg-store" as *u8, 15)
16 let cr: i64 = ss_commit(P, w, segid)
17 tot=tot+1; if cr >= 0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
18 gw("T1 commit (segid=" as *u8); gn(cr); gw(")\n" as *u8)
19
20 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
21 let r: i64 = ss_get(P, "k1" as *u8, pq, lq)
22 tot=tot+1; if r==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
23 gw("T2 get k1 -> found\n" as *u8)
24
25 let val: *u8 = pq[0] as *u8; let vl: i64 = lq[0]; let exp: *u8 = "hello-seg-store" as *u8
26 var m: i64=0; if vl==15 { m=1; var c: i64=0; while c<15 { if val[c]!=exp[c] { m=0 } c=c+1 } }
27 tot=tot+1; if m==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
28 gw("T3 value byte-exact (hello-seg-store)\n" as *u8)
29
30 let r2: i64 = ss_get(P, "nope" as *u8, pq, lq)
31 tot=tot+1; if r2 < 1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
32 gw("T4 absent key -> not found\n" as *u8)
33
34 // T5 MMAP-SERVE EQUIVALENCE: ss_open2(P,1) file-backed maps must return the SAME bytes as ss_open (read).
35 let hr: *i64 = ss_open(P) // read-all mode
36 let hm: *i64 = ss_open2(P, 1) // file-backed mmap mode
37 let pr: *i64 = sys_mmap(16) as *i64; let lr: *i64 = sys_mmap(16) as *i64
38 let pm: *i64 = sys_mmap(16) as *i64; let lm2: *i64 = sys_mmap(16) as *i64
39 let gr: i64 = ss_hget(hr, "k1" as *u8, pr, lr)
40 let gm: i64 = ss_hget(hm, "k1" as *u8, pm, lm2)
41 var eqm: i64 = 0
42 if gr == 1 { if gm == 1 { if lr[0] == lm2[0] {
43 eqm = 1
44 let ar: *u8 = pr[0] as *u8; let am: *u8 = pm[0] as *u8
45 var c5: i64 = 0
46 while c5 < lr[0] { if ar[c5] != am[c5] { eqm = 0; c5 = lr[0] } else { c5 = c5 + 1 } }
47 } } }
48 tot=tot+1; if eqm==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
49 gw("T5 mmap-open byte-identical to read-open (ss_open2 usemmap=1)\n" as *u8)
50
51 // ---- T6-T9 IMPACT-ORDERED POSTINGS (.imp sidecar, the WAND rung 2026-07-25) ----
52 // Own prefix: every segment under it is born with .imp (the legacy ssgate-test prefix carries
53 // pre-WAND segments, which would trip the all-or-nothing -3 by design).
54 let P2: *u8 = "knowledge/ssgimp-" as *u8
55 let segid2: i64 = ss_next_segid(P2)
56 let w2: *i64 = ss_begin()
57 ss_add(w2, 1, "ka" as *u8, "zebraq" as *u8, 6)
58 ss_add(w2, 1, "kb" as *u8, "zebraq zebraq zebraq zebraq zebraq" as *u8, 34)
59 ss_add(w2, 1, "kc" as *u8, "zebraq zebraq zebraq" as *u8, 20)
60 ss_commit(P2, w2, segid2)
61 let h6: *i64 = ss_open(P2)
62 let kp6: *i64 = sys_mmap(8 * 8) as *i64
63 let kl6: *i64 = sys_mmap(8 * 8) as *i64
64 let tf6: *i64 = sys_mmap(8 * 8) as *i64
65 let sat6: *i64 = sys_mmap(16) as *i64
66 // T6: max=2 keeps the HIGHEST-tf docs (kb tf=5, kc tf=3) -- the legacy ascending cap would keep
67 // ka (doc 0) and kb. This is the discriminating candidacy tooth for seq606.
68 let n6: i64 = ss_term_top(P2, h6, "zebraq" as *u8, kp6, kl6, tf6, 2, sat6)
69 let sat_t6: i64 = sat6[0]
70 var ok6: i64 = 0
71 if n6 == 2 { if kl6[0] == 2 { if kl6[1] == 2 {
72 let ka6: *u8 = kp6[0] as *u8
73 let kb6: *u8 = kp6[1] as *u8
74 if ka6[0] == (107 as u8) { if ka6[1] == (98 as u8) { if kb6[0] == (107 as u8) { if kb6[1] == (99 as u8) { ok6 = 1 } } } }
75 } } }
76 tot=tot+1; if ok6==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
77 gw("T6 impact candidacy: cap=2 keeps kb(tf5),kc(tf3) not ascending ka,kb\n" as *u8)
78 // T7: returned tf values are the true occurrence counts
79 var ok7: i64 = 0
80 if n6 == 2 { if tf6[0] == 5 { if tf6[1] == 3 { ok7 = 1 } } }
81 tot=tot+1; if ok7==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
82 gw("T7 impact tf values true (5,3)\n" as *u8)
83 // T8: fallback contract -- hide this run's .imp: ss_term_top must return -3 (all-or-nothing) and
84 // legacy ss_term must still answer; then the sidecar is RESTORED (and that restore must succeed,
85 // or every later run would inherit a -3 shard).
86 let hidef: *u8 = sys_mmap(512)
87 let realf: *u8 = sys_mmap(512)
88 ss_auxname(P2, segid2, ".imp" as *u8, 0, realf)
89 ss_auxname(P2, segid2, ".hid" as *u8, 0, hidef)
90 sys_renameat(realf, hidef)
91 let n8: i64 = ss_term_top(P2, h6, "zebraq" as *u8, kp6, kl6, tf6, 2, sat6)
92 let n8b: i64 = ss_term(h6, "zebraq" as *u8, kp6, kl6, 2)
93 let rr8: i64 = sys_renameat(hidef, realf)
94 var ok8: i64 = 0
95 if n8 == (0 - 3) { if n8b == 2 { if rr8 == 0 { ok8 = 1 } } }
96 tot=tot+1; if ok8==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
97 gw("T8 absent .imp -> -3 fallback, legacy path answers, sidecar restored\n" as *u8)
98 // T9: currency -- a NEW version of kb without the term must vanish from impact results (the
99 // live-doc map filters the stale high-tf posting exactly like ss_term does).
100 let segid3: i64 = ss_next_segid(P2)
101 let w3: *i64 = ss_begin()
102 ss_add(w3, 1, "kb" as *u8, "plainword here" as *u8, 14)
103 ss_commit(P2, w3, segid3)
104 let h9: *i64 = ss_open(P2)
105 let n9: i64 = ss_term_top(P2, h9, "zebraq" as *u8, kp6, kl6, tf6, 8, sat6)
106 var ok9: i64 = 0
107 if n9 == 2 { if kl6[0] == 2 { if kl6[1] == 2 {
108 let ka9: *u8 = kp6[0] as *u8
109 let kb9: *u8 = kp6[1] as *u8
110 if ka9[0] == (107 as u8) { if ka9[1] == (99 as u8) { if kb9[0] == (107 as u8) { if kb9[1] == (97 as u8) { ok9 = 1 } } } }
111 } } }
112 tot=tot+1; if ok9==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
113 gw("T9 currency: shadowed kb drops out; kc(tf3),ka(tf1) remain in impact order\n" as *u8)
114 // T10 saturation flag both ways: the T6 call truncated at max=2 (3 live matches) -> sat=1; the T9
115 // call returned every live match (2 <= max=8) -> sat=0. This is the honest-total rung's foundation.
116 var ok10: i64 = 0
117 if sat_t6 == 1 { if sat6[0] == 0 { ok10 = 1 } }
118 tot=tot+1; if ok10==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
119 gw("T10 truncation flag: capped call sat=1, complete call sat=0\n" as *u8)
120
121 // ---- seq1730 teeth: the pointer-shaped-segid guard must BITE, and bite FOR THE RIGHT REASON ------
122 // T11 ALONE WOULD BE VACUOUS: a store that refused every write would pass it too. T12 is the negative
123 // control -- identical writer shape, ONLY the segid differs -- so T11+T12 together isolate the SEGID
124 // as the cause of the refusal. Both prefixes are re-run safe: T11's can never gain a manifest (every
125 // commit to it is refused by construction), so the gate is idempotent (rule 10).
126 let PPOI: *u8 = "/tmp/nx_ssgate_poison-" as *u8
127 let PGOOD: *u8 = "/tmp/nx_ssgate_good-" as *u8
128 let want: i64 = 0 - 7
129 let wp: *i64 = ss_begin()
130 ss_add(wp, 1, "poison" as *u8, "should never land" as *u8, 17)
131 let rcp: i64 = ss_commit(PPOI, wp, 140712850411539)
132 var ok11: i64 = 0
133 if rcp == want { if ss_next_segid(PPOI) == 0 { ok11 = 1 } }
134 tot=tot+1; if ok11==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
135 gw("T11 pointer-shaped segid REFUSED (rc=-7) and NOTHING written\n" as *u8)
136
137 let wg: *i64 = ss_begin()
138 ss_add(wg, 1, "good" as *u8, "must land" as *u8, 9)
139 let rcg: i64 = ss_commit(PGOOD, wg, 1785516000000000)
140 var ok12: i64 = 0
141 if rcg == 0 { ok12 = 1 }
142 tot=tot+1; if ok12==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
143 gw("T12 NEG-CONTROL: legitimate epoch-us segid COMMITS (so T11 refused the ID, not the write)\n" as *u8)
144
145 var ok13: i64 = 1
146 if ss_segid_ok(135251248111617) != 0 { ok13 = 0 }
147 if ss_segid_ok(139871015653377) != 0 { ok13 = 0 }
148 if ss_segid_ok(139925199040513) != 0 { ok13 = 0 }
149 if ss_segid_ok(140190138056705) != 0 { ok13 = 0 }
150 if ss_segid_ok(140712850411538) != 0 { ok13 = 0 }
151 if ss_segid_ok(140712850411539) != 0 { ok13 = 0 }
152 if ss_segid_ok(0) != 1 { ok13 = 0 }
153 if ss_segid_ok(1) != 1 { ok13 = 0 }
154 if ss_segid_ok(1001) != 1 { ok13 = 0 }
155 if ss_segid_ok(1785516000) != 1 { ok13 = 0 }
156 if ss_segid_ok(1785516000000) != 1 { ok13 = 0 }
157 if ss_segid_ok(1785516000000000) != 1 { ok13 = 0 }
158 tot=tot+1; if ok13==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
159 gw("T13 boundary: all 6 OBSERVED poison ids refused; counters + epoch sec/ms/us all accepted\n" as *u8)
160
161 // T14 (seq1730): PROVE THE GUARD IS AT THE RIGHT DEPTH. Compactors call ss_write_seg DIRECTLY and
162 // hand-write the manifest, never touching ss_commit -- so T11 (which goes through ss_commit) cannot
163 // detect a hole on that path. This tooth exercises it, and also accepts a legitimate id so it cannot
164 // pass by refusing everything. Re-run safe: the poison prefix never gains a manifest.
165 let PCOMP: *u8 = "/tmp/nx_ssgate_compactor-" as *u8
166 let want14: i64 = 0 - 7
167 let wc: *i64 = ss_begin()
168 ss_add(wc, 1, "compacted" as *u8, "the compactor path" as *u8, 18)
169 var ok14: i64 = 0
170 if ss_write_seg(PCOMP, wc, 140712850411539) == want14 { ok14 = 1 }
171 let wc2: *i64 = ss_begin()
172 ss_add(wc2, 1, "compacted" as *u8, "the compactor path" as *u8, 18)
173 if ss_write_seg(PCOMP, wc2, 1785516000000001) != 0 { ok14 = 0 }
174 tot=tot+1; if ok14==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
175 gw("T14 COMPACTOR PATH: ss_write_seg (bypasses ss_commit) refuses the poison AND accepts a real id\n" as *u8)
176
177 gw("\n=== nx_seg_store_gate " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ===\n" as *u8)
178 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
179 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
180 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
181 let ctr__dry: *i64 = gv_ctr()
182 ctr__dry[0] = pass
183 ctr__dry[1] = tot
184 let rc__dry: i64 = gv_verdict("SEG-STORE-GATE" as *u8, ctr__dry, "append-only put/get round-trip byte-exact" as *u8)
185 sys_exit(rc__dry)
186 return rc__dry
187}