nx_memindex_emit_gate.nx source
↩ module page · 101 lines · 4782 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
11func 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 }
12func g_putn(v: i64) -> i64 { nxi_out(v); return 0 }
13func g_bool(name: *u8, got: i64, want: i64, passp: *i64) -> i64 {
14 g_puts("T " as *u8); g_puts(name); g_puts(" got=" as *u8); g_putn(got)
15 if got == want { g_puts(" PASS\n" as *u8); passp[0] = passp[0] + 1 } else { g_puts(" FAIL\n" as *u8) }
16 return 0
17}
18func 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 }
19func g_has(hay: *u8, hn: i64, needle: *u8) -> i64 {
20 let nn: i64 = ccz_slen(needle)
21 if nn == 0 { return 0 }
22 var i: i64 = 0
23 while i + nn <= hn {
24 var k: i64 = 0
25 var ok: i64 = 1
26 while k < nn { if hay[i+k] != needle[k] { ok = 0; k = nn } k = k + 1 }
27 if ok == 1 { return 1 }
28 i = i + 1
29 }
30 return 0
31}
32// fork elf with 2 args, wait
33func g_run(elf: *u8, a1: *u8, a2: *u8) -> i64 {
34 let pid: i64 = sys_fork()
35 if pid < 0 { return 127 }
36 if pid == 0 {
37 let av: *i64 = sys_mmap(64) as *i64
38 av[0] = elf as i64
39 av[1] = a1 as i64
40 av[2] = a2 as i64
41 av[3] = 0
42 sys_execve(elf, av, 0 as *i64)
43 sys_exit(127)
44 }
45 let st: *i64 = sys_mmap(16) as *i64
46 sys_wait4(pid, st, 0)
47 return wait_exit_code(st[0])
48}
49func g_same(p: *u8, q: *u8, qn: i64) -> i64 {
50 let b: *u8 = sys_mmap(262144)
51 let n: i64 = ccz_read(p, b, 262143)
52 if n != qn { return 0 }
53 var i: i64 = 0
54 while i < n { if b[i] != q[i] { return 0 } i = i + 1 }
55 return 1
56}
57
58func main(argc: i64, argv: *i64) -> i64 {
59 var elf: *u8 = "_build/nx_memindex_emit.sov.elf" as *u8
60 if argc >= 2 { elf = argv[1] as *u8 }
61 let pass: *i64 = sys_mmap(16) as *i64
62 pass[0] = 0
63
64 // fixture journal: slug law v1, then v2 (supersedes)
65 g_write("/tmp/mie.journal" as *u8, "law entry LAW vONE\nother entry OTHER stays\nlaw entry LAW vTWO\n" as *u8)
66 // fixture md with markers + curated content around
67 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)
68
69 let rc: i64 = g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie.md" as *u8)
70 g_bool("emit-exit0" as *u8, rc, 0, pass)
71
72 let buf: *u8 = sys_mmap(262144)
73 let n: i64 = ccz_read("/tmp/mie.md" as *u8, buf, 262143)
74 g_bool("derived-vTWO-in" as *u8, g_has(buf, n, "entry LAW vTWO" as *u8), 1, pass)
75 // NEG-CONTROL 1: superseded vONE absent + stale prior section content gone
76 var okneg: i64 = 0
77 if g_has(buf, n, "entry LAW vONE" as *u8) == 0 { if g_has(buf, n, "stale old derived" as *u8) == 0 { okneg = 1 } }
78 g_bool("negctl-superseded+stale-absent" as *u8, okneg, 1, pass)
79 // curation preserved on both sides of the markers
80 var okcur: i64 = 0
81 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 } } }
82 g_bool("curation-preserved" as *u8, okcur, 1, pass)
83
84 // NEG-CONTROL 2: a file WITHOUT markers is untouched byte-identical
85 g_write("/tmp/mie_nomark.md" as *u8, "# no markers here NOMARKSENTINEL\n- body\n" as *u8)
86 let before: *u8 = sys_mmap(4096)
87 let bn: i64 = ccz_read("/tmp/mie_nomark.md" as *u8, before, 4095)
88 g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie_nomark.md" as *u8)
89 g_bool("negctl-nomarkers-untouched" as *u8, g_same("/tmp/mie_nomark.md" as *u8, before, bn), 1, pass)
90
91 // idempotent: second splice run -> byte-identical
92 let snap: *u8 = sys_mmap(262144)
93 let sn: i64 = ccz_read("/tmp/mie.md" as *u8, snap, 262143)
94 g_run(elf, "/tmp/mie.journal" as *u8, "/tmp/mie.md" as *u8)
95 g_bool("idempotent" as *u8, g_same("/tmp/mie.md" as *u8, snap, sn), 1, pass)
96
97 g_puts("MIE-GATE pass=" as *u8); g_putn(pass[0]); g_puts("/6 verdict=" as *u8)
98 if pass[0] == 6 { g_puts("GREEN\n" as *u8); return 0 }
99 g_puts("RED\n" as *u8)
100 return 1
101}