code wiki / _hdl_build / nx_teacher_merge_gate.nx
nx_teacher_merge_gate.nx source
↩ module page · 130 lines · 7080 B
1// nx_teacher_merge_gate.nx -- proves MULTI-SOURCE synthesis into one unified teacher capability store:
2// heterogeneous-schema ledgers (different rule columns) unify, cross-source DEDUP works, re-merge is
3// idempotent, and redundancy is per-source. Hermetic /tmp store. Discriminating + liar-killed.
4//
5// T1 merge ledger A (rule@col2, 3 rows) -> count 3
6// T2 merge ledger B (rule@col1, DIFFERENT schema, 3 rows) -> count 6 (2nd source did not clobber 1st)
7// T3 recall A2 from the unified store == faithful (consume across sources)
8// T4 recall B2 from the unified store == faithful
9// T5 redundancy: ledger A fully recoverable (rule_col=2)
10// T6 redundancy: ledger B fully recoverable (rule_col=1)
11// T7[neg] ledger C (shares A2's rule + 1 new) is NOT redundant BEFORE merge (the new rule is absent)
12// T8 re-merge A -> 0 added (cross-source dedup => idempotent)
13// T9 count still 6 after the idempotent re-merge
14// T10 merge C -> exactly 1 added (only the genuinely-new rule; the shared one deduped)
15// T11 count == 7
16// T12 ledger C now redundant (both its rules recoverable)
17// T13[neg] no-fabrication: a rule never merged is ABSENT from the store
18// GREEN iff all. Sovereign: imports nx_teacher_merge + nx_syscalls. license_tier: ORIGINAL
19import "nx_teacher_merge.nx"
20import "nx_syscalls.nx"
21
22func mg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 }
23func mg_putn(v: i64) -> i64 {
24 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
25 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
26 let d: *u8 = sys_mmap(24); var k: i64 = 0
27 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
28 var j: i64 = k - 1
29 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
30 return 0
31}
32func mg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != 0 as u8 { dst[off + i] = s[i]; i = i + 1 } return off + i }
33func mg_catn(dst: *u8, off: i64, v: i64) -> i64 {
34 var m: i64 = v; var o: i64 = off
35 let t: *u8 = sys_mmap(28); var k: i64 = 0
36 if m == 0 { t[0] = 48 as u8; k = 1 }
37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
38 var i: i64 = 0; while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
39 return o + k
40}
41func mg_join(out: *u8, a: *u8, b: *u8) -> i64 { var o: i64 = mg_cat(out, 0, a); o = mg_cat(out, o, b); out[o] = 0 as u8; return o }
42func mg_mkdir(path: *u8) -> i64 { return sys_mkdir(path, 0x1ed) }
43func mg_write(path: *u8, content: *u8) -> i64 {
44 let fd: i64 = sys_openat_wr(path, 0x1a4)
45 if fd < 0 { return 0 - 1 }
46 var n: i64 = 0; while content[n] != 0 as u8 { n = n + 1 }
47 sys_write(fd, content, n); sys_close(fd); return 0
48}
49func mg_count(store: *u8) -> i64 {
50 let h: *i64 = ncfg_open(store)
51 if (h as i64) == 0 { return 0 }
52 return ncfg_count(h, "insight\x00" as *u8)
53}
54func mg_assert(label: *u8, cond: i64) -> i64 {
55 if cond != 0 { mg_puts(" [PASS] "); mg_puts(label); mg_puts("\n"); return 1 }
56 mg_puts(" [FAIL] "); mg_puts(label); mg_puts("\n"); return 0
57}
58
59func main() -> i64 {
60 mg_puts("=== nx_teacher_merge_gate: multi-source unify + cross-source dedup + idempotency (liar-killed) ===\n")
61 let epoch: i64 = sys_now_realtime_sec()
62 let root: *u8 = sys_mmap(512)
63 var ro: i64 = mg_cat(root, 0, "/tmp/tmg-" as *u8); ro = mg_catn(root, ro, epoch); root[ro] = 0 as u8
64 mg_mkdir(root)
65 let A: *u8 = sys_mmap(512); mg_join(A, root, "/a.tsv\x00" as *u8)
66 let B: *u8 = sys_mmap(512); mg_join(B, root, "/b.tsv\x00" as *u8)
67 let C: *u8 = sys_mmap(512); mg_join(C, root, "/c.tsv\x00" as *u8)
68 let store: *u8 = sys_mmap(512); mg_join(store, root, "/store-\x00" as *u8)
69
70 // ledger A: rule @ col 2 (id<TAB>class<TAB>rule<TAB>x)
71 mg_write(A, "# A\nA1\tCHEAT\talpha one\tx\nA2\tPROC\talpha two\tx\nA3\tDRY\talpha three\tx\n\x00" as *u8)
72 // ledger B: rule @ col 1 (id<TAB>rule) -- different schema
73 mg_write(B, "# B\nB1\tbeta one\nB2\tbeta two\nB3\tbeta three\n\x00" as *u8)
74 // ledger C: rule @ col 2; C1 duplicates A2's rule, C2 is genuinely new
75 mg_write(C, "# C\nC1\tCLS\talpha two\tx\nC2\tCLS\tgamma new\tx\n\x00" as *u8)
76
77 var pass: i64 = 0
78 var total: i64 = 0
79 var c: i64 = 0
80
81 let addA: i64 = tsyn_merge_cols(A, "srcA\x00" as *u8, store, 0, 2)
82 c = 0; if addA == 3 { if mg_count(store) == 3 { c = 1 } }
83 total = total + 1; pass = pass + mg_assert("T1 merge A (rule@2) -> +3, count 3", c)
84
85 let addB: i64 = tsyn_merge_cols(B, "srcB\x00" as *u8, store, 0, 1)
86 c = 0; if addB == 3 { if mg_count(store) == 6 { c = 1 } }
87 total = total + 1; pass = pass + mg_assert("T2 merge B (rule@1, diff schema) -> +3, count 6 (no clobber)", c)
88
89 let oa: *u8 = sys_mmap(256)
90 c = 0; if tsyn_recall(store, "A2\x00" as *u8, oa) == 1 { if tsyn_streq(oa, "alpha two\x00" as *u8) == 1 { c = 1 } }
91 total = total + 1; pass = pass + mg_assert("T3 recall A2 from unified store == faithful", c)
92
93 let ob: *u8 = sys_mmap(256)
94 c = 0; if tsyn_recall(store, "B2\x00" as *u8, ob) == 1 { if tsyn_streq(ob, "beta two\x00" as *u8) == 1 { c = 1 } }
95 total = total + 1; pass = pass + mg_assert("T4 recall B2 from unified store == faithful", c)
96
97 c = 0; if tsyn_redundant_col(A, store, 2) == 1 { c = 1 }
98 total = total + 1; pass = pass + mg_assert("T5 ledger A redundant (rule_col=2)", c)
99
100 c = 0; if tsyn_redundant_col(B, store, 1) == 1 { c = 1 }
101 total = total + 1; pass = pass + mg_assert("T6 ledger B redundant (rule_col=1)", c)
102
103 c = 0; if tsyn_redundant_col(C, store, 2) == 0 { c = 1 }
104 total = total + 1; pass = pass + mg_assert("T7[neg] ledger C NOT redundant before merge (new rule absent)", c)
105
106 let addA2: i64 = tsyn_merge_cols(A, "srcA\x00" as *u8, store, 0, 2)
107 c = 0; if addA2 == 0 { c = 1 }
108 total = total + 1; pass = pass + mg_assert("T8 re-merge A -> 0 added (cross-source dedup => idempotent)", c)
109
110 c = 0; if mg_count(store) == 6 { c = 1 }
111 total = total + 1; pass = pass + mg_assert("T9 count still 6 after idempotent re-merge", c)
112
113 let addC: i64 = tsyn_merge_cols(C, "srcC\x00" as *u8, store, 0, 2)
114 c = 0; if addC == 1 { c = 1 }
115 total = total + 1; pass = pass + mg_assert("T10 merge C -> exactly 1 added (shared rule deduped, new one kept)", c)
116
117 c = 0; if mg_count(store) == 7 { c = 1 }
118 total = total + 1; pass = pass + mg_assert("T11 count == 7", c)
119
120 c = 0; if tsyn_redundant_col(C, store, 2) == 1 { c = 1 }
121 total = total + 1; pass = pass + mg_assert("T12 ledger C now redundant (both rules recoverable)", c)
122
123 let h: *i64 = ncfg_open(store)
124 c = 0; if tsyn_store_has_rule(h, "PHANTOM never merged insight\x00" as *u8) == 0 { c = 1 }
125 total = total + 1; pass = pass + mg_assert("T13[neg] no-fabrication: a rule never merged is ABSENT", c)
126
127 mg_puts("\n---- nx_teacher_merge gate: passed "); mg_putn(pass); mg_puts(" / "); mg_putn(total); mg_puts(" ----\n")
128 if pass == total { mg_puts("verdict=GREEN\n"); sys_exit(0); return 0 }
129 mg_puts("verdict=RED\n"); sys_exit(1); return 1
130}