nx_memindex_emit_gate.nx source
↩ module page · 141 lines · 7605 B
1// nx_memindex_emit_gate.nx -- liar-killed GATE for the MEMORY.md coindex splice organ. Forks the
2// REAL built elf (_build/nx_memindex_emit.sov.elf) against fixtures and proves: derived entries land
3// between the markers with last-write-wins (superseded entry ABSENT -- neg-control); everything
4// OUTSIDE the markers is preserved BYTE-IDENTICAL (curation never harmed); a file WITHOUT markers
5// is left byte-identical (fail-closed, the second neg-control); the splice is IDEMPOTENT (second
6// run byte-identical). usage: nx_memindex_emit_gate [emit_elf] Exit 0 only on 6/6.
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9import "nx_crashresume_census_core.nx"
10// D001 MIGRATION 2026-08-16. This gate was BUILT-UNPROMOTED -- compiled, never deployed, so the organ
11// it guards had no runnable gate at all, and the D001 refusal is what kept it that way. Its teeth were
12// good; only its bookkeeping was hand-rolled.
13// ★★★★★THE HAND-ROLLED DENOMINATOR IS THE TELL: it printed `pass=N/6` with the 6 TYPED IN, so adding
14// a seventh tooth would have printed `7/6` and removing one would still have read GREEN at 6. gv_ctr
15// counts both sides, so declared == executed BY CONSTRUCTION.
16import "nx_gate_verdict.nx"
17
18func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
19func g_putn(v: i64) -> i64 { nxi_out(v); return 0 }
20func g_bool(name: *u8, got: i64, want: i64, passp: *i64) -> i64 {
21 var ok: i64 = 0
22 if got == want { ok = 1 }
23 let r: i64 = gv_check(name, ok, passp)
24 g_puts(" got=" as *u8); g_putn(got); g_puts(" want=" as *u8); g_putn(want); g_puts("\n" as *u8)
25 return r
26}
27func g_write(path: *u8, s: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 420); if fd < 0 { return 0 - 1 } sys_write(fd, s, ccz_slen(s)); sys_close(fd); return 0 }
28func g_has(hay: *u8, hn: i64, needle: *u8) -> i64 {
29 let nn: i64 = ccz_slen(needle)
30 if nn == 0 { return 0 }
31 var i: i64 = 0
32 while i + nn <= hn {
33 var k: i64 = 0
34 var ok: i64 = 1
35 while k < nn { if hay[i+k] != needle[k] { ok = 0; k = nn } k = k + 1 }
36 if ok == 1 { return 1 }
37 i = i + 1
38 }
39 return 0
40}
41// fork elf with 2 args, wait
42func g_run(elf: *u8, a1: *u8, a2: *u8) -> i64 {
43 let pid: i64 = sys_fork()
44 if pid < 0 { return 127 }
45 if pid == 0 {
46 let av: *i64 = sys_mmap(64) as *i64
47 av[0] = elf as i64
48 av[1] = a1 as i64
49 av[2] = a2 as i64
50 av[3] = 0
51 sys_execve(elf, av, 0 as *i64)
52 sys_exit(127)
53 }
54 let st: *i64 = sys_mmap(16) as *i64
55 sys_wait4(pid, st, 0)
56 return wait_status_rc(st[0])
57}
58// ⚠A RELATIVE SUBJECT PATH IS RESOLVED AGAINST THE CALLER'S CWD, AND THIS GATE HAS TWO CALLERS WITH
59// DIFFERENT ONES: nx_sov_build_run anchors CWD to buildroot/ (so `_build/...` resolves) while
60// /api/gate_run runs from the SERVING ROOT (where it does not). MEASURED 2026-08-16: the fork returned
61// 127 -- exec NOT-FOUND -- and the gate still reported 3/6 PASS.
62// ★★★★★★AN ABSENT SUBJECT MAKES EVERY "UNCHANGED" ASSERTION TRIVIALLY TRUE, SO A GATE CAN REPORT
63// PASSES IT DID NOT EARN: curation-preserved, nomarkers-untouched and idempotent ALL passed precisely
64// BECAUSE nothing ran. The failing teeth were the only honest ones.
65// ⇒ STAT the candidates, never assume; and ABSTAIN (gv_need -> SKIP) rather than run teeth against a
66// binary that is not there -- "I could not look" is not "it is broken", and it is REALLY not "it works".
67func g_exists(p: *u8) -> i64 {
68 let b: *u8 = sys_mmap(64)
69 let n: i64 = ccz_read(p, b, 8)
70 sys_munmap(b, 64)
71 if n > 0 { return 1 }
72 return 0
73}
74func g_same(p: *u8, q: *u8, qn: i64) -> i64 {
75 let b: *u8 = sys_mmap(262144)
76 let n: i64 = ccz_read(p, b, 262143)
77 if n != qn { return 0 }
78 var i: i64 = 0
79 while i < n { if b[i] != q[i] { return 0 } i = i + 1 }
80 return 1
81}
82
83func main(argc: i64, argv: *i64) -> i64 {
84 var elf: *u8 = "buildroot/_build/nx_memindex_emit.sov.elf" as *u8
85 if argc >= 2 { elf = argv[1] as *u8 } else {
86 // Candidate roots in the order that answers the question the gate is FOR: the freshest build
87 // first, then the deployed artifacts. Each is STAT'd, never assumed.
88 if g_exists(elf) == 0 { elf = "_build/nx_memindex_emit.sov.elf" as *u8 }
89 if g_exists(elf) == 0 { elf = "_offc/nx_memindex_emit.elf" as *u8 }
90 if g_exists(elf) == 0 { elf = "nx_memindex_emit.elf" as *u8 }
91 }
92 let pass: *i64 = gv_ctr()
93 gv_head("MIE-GATE (MEMORY.md coindex splice: last-write-wins, curation preserved, fail-closed, idempotent)" as *u8)
94 if gv_need("subject-elf-resolvable" as *u8, g_exists(elf), pass) == 0 {
95 g_puts(" looked for: buildroot/_build/ then _build/ then _offc/ then the serving root.\n" as *u8)
96 g_puts(" Running the teeth anyway would report PASS for every 'left untouched' assertion.\n" as *u8)
97 let rc0: i64 = gv_verdict("MIE-GATE" as *u8, pass, "MEMORY.md coindex splice over the built nx_memindex_emit elf" as *u8)
98 sys_exit(rc0)
99 return rc0
100 }
101 g_puts(" subject=" as *u8); g_puts(elf); g_puts("\n" as *u8)
102
103 // fixture journal: slug law v1, then v2 (supersedes)
104 g_write("/tmp/mie.journal" as *u8, "law entry LAW vONE\nother entry OTHER stays\nlaw entry LAW vTWO\n" as *u8)
105 // fixture md with markers + curated content around
106 g_write("/tmp/mie.md" as *u8, "# header TOPSENTINEL\n## LIVE\n<!-- COINDEX:BEGIN -->\nstale old derived\n<!-- COINDEX:END -->\n## curated BOTTOMSENTINEL\n- keep me\n" as *u8)
107
108 let rc: i64 = g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie.md" as *u8)
109 g_bool("emit-exit0" as *u8, rc, 0, pass)
110
111 let buf: *u8 = sys_mmap(262144)
112 let n: i64 = ccz_read("/tmp/mie.md" as *u8, buf, 262143)
113 g_bool("derived-vTWO-in" as *u8, g_has(buf, n, "entry LAW vTWO" as *u8), 1, pass)
114 // NEG-CONTROL 1: superseded vONE absent + stale prior section content gone
115 var okneg: i64 = 0
116 if g_has(buf, n, "entry LAW vONE" as *u8) == 0 { if g_has(buf, n, "stale old derived" as *u8) == 0 { okneg = 1 } }
117 g_bool("neg-control-superseded+stale-absent" as *u8, okneg, 1, pass)
118 // curation preserved on both sides of the markers
119 var okcur: i64 = 0
120 if g_has(buf, n, "TOPSENTINEL" as *u8) == 1 { if g_has(buf, n, "BOTTOMSENTINEL" as *u8) == 1 { if g_has(buf, n, "- keep me" as *u8) == 1 { okcur = 1 } } }
121 g_bool("curation-preserved" as *u8, okcur, 1, pass)
122
123 // NEG-CONTROL 2: a file WITHOUT markers is untouched byte-identical
124 g_write("/tmp/mie_nomark.md" as *u8, "# no markers here NOMARKSENTINEL\n- body\n" as *u8)
125 let before: *u8 = sys_mmap(4096)
126 let bn: i64 = ccz_read("/tmp/mie_nomark.md" as *u8, before, 4095)
127 g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie_nomark.md" as *u8)
128 g_bool("neg-control-nomarkers-untouched" as *u8, g_same("/tmp/mie_nomark.md" as *u8, before, bn), 1, pass)
129
130 // idempotent: second splice run -> byte-identical
131 let snap: *u8 = sys_mmap(262144)
132 let sn: i64 = ccz_read("/tmp/mie.md" as *u8, snap, 262143)
133 g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie.md" as *u8)
134 g_bool("idempotent" as *u8, g_same("/tmp/mie.md" as *u8, snap, sn), 1, pass)
135
136 // ★A GATE WHOSE EXIT CODE DOES NOT CARRY ITS VERDICT SILENTLY BLESSES EVERY FAILURE IT FINDS, and
137 // the `/6` above was a SECOND, HAND-TYPED denominator beside the real one.
138 let rc: i64 = gv_verdict("MIE-GATE" as *u8, pass, "MEMORY.md coindex splice over the built nx_memindex_emit elf" as *u8)
139 sys_exit(rc)
140 return rc
141}