code wiki / _hdl_build / nx_consolidate_gate.nx

nx_consolidate_gate.nx source

↩ module page · 131 lines · 7112 B

1// nx_consolidate_gate.nx -- GATE for the consolidation architect (6 teeth incl 2 negative controls 2// + determinism + a MUTATION proof). PREREQ: stage _offc/nx_consolidate.elf. Run from nxc2 root. 3// One consolidator fork per tooth (self-exec chain). Fixture trees /tmp/cdA + /tmp/cdB with: 4// same.nx = byte-identical in both -> IDENTICAL (safe collapse) 5// diff.nx = different bytes -> DIVERGENT (hazard, never auto-collapse) 6// only.nx = present but census names an absent tree copy -> MISSING (honest) 7// T1 identical->IDENTICAL w=5 | T2 NEG divergent->DIVERGENT w=8 (behaviour would change) | 8// T3 MISSING honest | T4 summary pairs=3 identical=1 divergent=1 missing=1 | T5 determinism (two 9// runs seed byte-identical consolq) | T6 MUTATION: append a byte to A/same.nx -> IDENTICAL flips to 10// DIVERGENT (the byte-equal gate genuinely discriminates; restore not needed, /tmp is scratch). 11// license_tier: ORIGINAL No hw writes (Rule 26). 12import "nx_seat_drive_lib.nx" 13import "nx_store_seed_lib.nx" 14import "nx_seg_store.nx" 15import "nx_deploy_lib.nx" 16import "nx_syscalls.nx" 17 18func cg_atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } } i = i + 1 } return v } 19func cg_itoa(dst: *u8, v: i64) -> i64 { var m: i64 = v; let t: *u8 = sys_mmap(24); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { dst[i] = t[k - 1 - i]; i = i + 1 } dst[k] = 0 as u8; return k } 20func cg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 21 22func cg_run(capp: *u8, cqp: *u8, outp: *u8) -> i64 { 23 let av: *i64 = sys_mmap(32) as *i64 24 av[0] = capp as i64 25 av[1] = cqp as i64 26 return dep_run_capture("_offc/nx_consolidate.elf" as *u8, av, 2, outp) 27} 28 29func main(argc: i64, argv: *i64) -> i64 { 30 var stage: i64 = 1 31 var pass: i64 = 0 32 if argc >= 2 { let ss1: *u8 = argv[1] as *u8; stage = cg_atoi(ss1) } 33 if argc >= 3 { let ps: *u8 = argv[2] as *u8; pass = cg_atoi(ps) } 34 if stage < 1 { stage = 1 } 35 36 sys_mkdir("/tmp/cdA" as *u8, 0x1ed) 37 sys_mkdir("/tmp/cdB" as *u8, 0x1ed) 38 let same: *u8 = "func f() -> i64 { return 42 }\n" as *u8 39 ss_writefile("/tmp/cdA/same.nx" as *u8, same, cg_len(same)) 40 ss_writefile("/tmp/cdB/same.nx" as *u8, same, cg_len(same)) 41 let da: *u8 = "func g() -> i64 { return 1 }\n" as *u8 42 let db: *u8 = "func g() -> i64 { return 2 }\n" as *u8 43 ss_writefile("/tmp/cdA/diff.nx" as *u8, da, cg_len(da)) 44 ss_writefile("/tmp/cdB/diff.nx" as *u8, db, cg_len(db)) 45 ss_writefile("/tmp/cdA/only.nx" as *u8, same, cg_len(same)) 46 // census fixture: 3 dup lines (same, diff, only) -- only.nx names /tmp/cdB where it is ABSENT 47 let cap0: *u8 = " DUP basename: same.nx (present in both /tmp/cdA and /tmp/cdB)\n DUP basename: diff.nx (present in both /tmp/cdA and /tmp/cdB)\n DUP basename: only.nx (present in both /tmp/cdA and /tmp/cdB)\n" as *u8 48 ss_writefile("/tmp/cd_census.txt" as *u8, cap0, cg_len(cap0)) 49 50 if stage == 1 { 51 let rc: i64 = cg_run("/tmp/cd_census.txt" as *u8, "/tmp/cd_q1-" as *u8, "/tmp/cd_t1.out" as *u8) 52 let out: *u8 = sys_mmap(65536) 53 let n: i64 = dp_read("/tmp/cd_t1.out" as *u8, out, 65536) 54 let qb: *u8 = sys_mmap(65536) 55 let qn: i64 = sts_load("/tmp/cd_q1-" as *u8, qb, 65536) 56 var ok: i64 = 0 57 if rc == 0 { if sd_count(out, n, "PROPOSE same.nx IDENTICAL" as *u8) == 1 { if sd_count(qb, qn, "[IDENTICAL" as *u8) == 1 { ok = 1 } } } 58 if ok == 1 { sd_w("T1 identical-safe-collapse PASS\n" as *u8); pass = pass + 1 } else { sd_w("T1 identical-safe-collapse FAIL\n" as *u8) } 59 } 60 if stage == 2 { 61 let out: *u8 = sys_mmap(65536) 62 let n: i64 = dp_read("/tmp/cd_t1.out" as *u8, out, 65536) 63 var ok: i64 = 0 64 if sd_count(out, n, "PROPOSE diff.nx DIVERGENT" as *u8) == 1 { ok = 1 } 65 if ok == 1 { sd_w("T2 neg-divergent-hazard PASS\n" as *u8); pass = pass + 1 } else { sd_w("T2 neg-divergent-hazard FAIL\n" as *u8) } 66 } 67 if stage == 3 { 68 let out: *u8 = sys_mmap(65536) 69 let n: i64 = dp_read("/tmp/cd_t1.out" as *u8, out, 65536) 70 var ok: i64 = 0 71 if sd_count(out, n, "PROPOSE only.nx MISSING" as *u8) == 1 { ok = 1 } 72 if ok == 1 { sd_w("T3 missing-honest PASS\n" as *u8); pass = pass + 1 } else { sd_w("T3 missing-honest FAIL\n" as *u8) } 73 } 74 if stage == 4 { 75 let out: *u8 = sys_mmap(65536) 76 let n: i64 = dp_read("/tmp/cd_t1.out" as *u8, out, 65536) 77 var ok: i64 = 0 78 if sd_count(out, n, "pairs=3 identical=1 divergent=1 missing=1" as *u8) == 1 { ok = 1 } 79 if ok == 1 { sd_w("T4 summary-exact PASS\n" as *u8); pass = pass + 1 } else { sd_w("T4 summary-exact FAIL\n" as *u8) } 80 } 81 if stage == 5 { 82 let rc: i64 = cg_run("/tmp/cd_census.txt" as *u8, "/tmp/cd_q3-" as *u8, "/tmp/cd_t5.out" as *u8) 83 let qa: *u8 = sys_mmap(65536) 84 let qan: i64 = sts_load("/tmp/cd_q1-" as *u8, qa, 65536) 85 let qc: *u8 = sys_mmap(65536) 86 let qcn: i64 = sts_load("/tmp/cd_q3-" as *u8, qc, 65536) 87 var same2: i64 = 0 88 if rc == 0 { if qan == qcn { if qan > 0 { same2 = 1; var i: i64 = 0; while i < qan { if qa[i] != qc[i] { same2 = 0; i = qan } else { i = i + 1 } } } } } 89 if same2 == 1 { sd_w("T5 deterministic-view PASS\n" as *u8); pass = pass + 1 } else { sd_w("T5 deterministic-view FAIL\n" as *u8) } 90 } 91 if stage == 6 { 92 // MUTATION: make same.nx differ in cdB -> IDENTICAL must flip to DIVERGENT 93 let mut: *u8 = "func f() -> i64 { return 42 }\nX\n" as *u8 94 ss_writefile("/tmp/cdB/same.nx" as *u8, mut, cg_len(mut)) 95 let rc: i64 = cg_run("/tmp/cd_census.txt" as *u8, "/tmp/cd_q6-" as *u8, "/tmp/cd_t6.out" as *u8) 96 let out: *u8 = sys_mmap(65536) 97 let n: i64 = dp_read("/tmp/cd_t6.out" as *u8, out, 65536) 98 var ok: i64 = 0 99 if rc == 0 { if sd_count(out, n, "PROPOSE same.nx DIVERGENT" as *u8) == 1 { if sd_count(out, n, "identical=0" as *u8) == 1 { ok = 1 } } } 100 if ok == 1 { sd_w("T6 mutation-flips-verdict PASS\n" as *u8); pass = pass + 1 } else { sd_w("T6 mutation-flips-verdict FAIL\n" as *u8) } 101 } 102 103 if stage >= 6 { 104 sd_w("NX-CONSOLIDATE-GATE pass=" as *u8) 105 let pb: *u8 = sys_mmap(8) 106 pb[0] = (48 + pass) as u8 107 pb[1] = 0 as u8 108 sd_w(pb) 109 if pass == 6 { sd_w("/6 verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 110 sd_w("/6 verdict=RED\n" as *u8) 111 sys_exit(1) 112 return 1 113 } 114 115 let self: *u8 = argv[0] as *u8 116 let sb: *u8 = sys_mmap(24) 117 cg_itoa(sb, stage + 1) 118 let pb2b: *u8 = sys_mmap(24) 119 cg_itoa(pb2b, pass) 120 let nav: *i64 = sys_mmap(40) as *i64 121 nav[0] = self as i64 122 nav[1] = sb as i64 123 nav[2] = pb2b as i64 124 nav[3] = 0 125 let envp: *i64 = sys_mmap(16) as *i64 126 envp[0] = 0 127 sys_execve(self, nav, envp) 128 sd_w("CDG-EXEC-FAIL\n" as *u8) 129 sys_exit(1) 130 return 1 131}