nx_coindex_gate.nx source
↩ module page · 81 lines · 3978 B
1// nx_coindex_gate.nx -- liar-killed GATE for the conflict-free co-edited index (R-ORCH-2).
2// Proves the append-and-derive property that KILLS the parallel-session merge-dance: many appends
3// (the concurrent-writer stand-in -- concurrency safety itself is inherited from the flock floor,
4// WMS R11 MEASURED 0/144-lost-vs-git-132) all land with NO loss; the reducer keeps LAST-write-wins
5// per slug (a session re-banking its entry supersedes its own prior, never another's); a fresh slug
6// is added, never colliding. Load-bearing neg-control: the superseded line MUST be absent from the
7// derived index (proves last-wins actually drops the stale entry, not just appends). Exit 0 on 6/6.
8// license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_coindex_core.nx"
11
12func 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 }
13func g_putn(v: i64) -> i64 { nxi_out(v); return 0 }
14func g_bool(name: *u8, got: i64, want: i64, passp: *i64) -> i64 {
15 g_puts("T " as *u8); g_puts(name); g_puts(" got=" as *u8); g_putn(got)
16 if got == want { g_puts(" PASS\n" as *u8); passp[0] = passp[0] + 1 } else { g_puts(" FAIL\n" as *u8) }
17 return 0
18}
19// substring present in a NUL-terminated buffer?
20func g_has(hay: *u8, needle: *u8) -> i64 {
21 let hn: i64 = ccz_slen(hay)
22 let nn: i64 = ccz_slen(needle)
23 if nn == 0 { return 0 }
24 var i: i64 = 0
25 while i + nn <= hn {
26 var k: i64 = 0
27 var ok: i64 = 1
28 while k < nn { if hay[i+k] != needle[k] { ok = 0; k = nn } k = k + 1 }
29 if ok == 1 { return 1 }
30 i = i + 1
31 }
32 return 0
33}
34func g_lines(s: *u8) -> i64 { let n: i64 = ccz_slen(s); var c: i64 = 0; var i: i64 = 0; while i < n { if s[i] == (10 as u8) { c = c + 1 } i = i + 1 } return c }
35
36func main(argc: i64, argv: *i64) -> i64 {
37 let pass: *i64 = sys_mmap(16) as *i64
38 pass[0] = 0
39 let jr: *u8 = "/tmp/ci_gate.journal" as *u8
40 let lk: *u8 = "/tmp/ci_gate.journal.lock" as *u8
41 // fresh journal
42 let z: i64 = sys_openat_wr(jr, 420); if z >= 0 { sys_close(z) }
43
44 // four appends: slug a, b, a-again (supersedes), c -- the "two sessions, one re-banks" case
45 ci_append(jr, lk, "sess-a" as *u8, "entry A version ONE" as *u8)
46 ci_append(jr, lk, "sess-b" as *u8, "entry B only" as *u8)
47 ci_append(jr, lk, "sess-a" as *u8, "entry A version TWO" as *u8)
48 ci_append(jr, lk, "sess-c" as *u8, "entry C only" as *u8)
49
50 // T1 NO LOSS: all 4 raw appends landed in the journal
51 g_bool("no-loss-4-appends" as *u8, ci_count(jr), 4, pass)
52
53 let out: *u8 = sys_mmap(65536)
54 ci_emit(jr, out, 65536)
55
56 // T2 derived index has exactly 3 lines (3 distinct slugs)
57 g_bool("derive-3-distinct-slugs" as *u8, g_lines(out), 3, pass)
58 // T3 last-write-wins: version TWO present
59 g_bool("last-wins-vTWO-present" as *u8, g_has(out, "entry A version TWO" as *u8), 1, pass)
60 // T4 LOAD-BEARING NEG-CONTROL: the superseded version ONE is ABSENT (last-wins really drops it)
61 g_bool("negctl-vONE-absent" as *u8, g_has(out, "entry A version ONE" as *u8), 0, pass)
62 // T5 the other sessions' entries are untouched (b and c both present -- no cross-session clobber)
63 var okbc: i64 = 0
64 if g_has(out, "entry B only" as *u8) == 1 { if g_has(out, "entry C only" as *u8) == 1 { okbc = 1 } }
65 g_bool("other-sessions-intact" as *u8, okbc, 1, pass)
66
67 // T6 determinism: a second emit is byte-identical
68 let out2: *u8 = sys_mmap(65536)
69 ci_emit(jr, out2, 65536)
70 var okdet: i64 = 1
71 var i: i64 = 0
72 let l1: i64 = ccz_slen(out)
73 let l2: i64 = ccz_slen(out2)
74 if l1 != l2 { okdet = 0 } else { while i < l1 { if out[i] != out2[i] { okdet = 0; i = l1 } i = i + 1 } }
75 g_bool("emit-deterministic" as *u8, okdet, 1, pass)
76
77 g_puts("COINDEX-GATE pass=" as *u8); g_putn(pass[0]); g_puts("/6 verdict=" as *u8)
78 if pass[0] == 6 { g_puts("GREEN\n" as *u8); return 0 }
79 g_puts("RED\n" as *u8)
80 return 1
81}