code wiki / _hdl_build / nx_teacher_merge_gate.nx
nx_teacher_merge_gate.nx source
↩ module page · 138 lines · 7539 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"
21import "nx_gate_verdict.nx"
22
23func 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 }
24func mg_putn(v: i64) -> i64 {
25 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
26 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
27 let d: *u8 = sys_mmap(24); var k: i64 = 0
28 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
29 var j: i64 = k - 1
30 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
31 return 0
32}
33func 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 }
34func mg_catn(dst: *u8, off: i64, v: i64) -> i64 {
35 var m: i64 = v; var o: i64 = off
36 let t: *u8 = sys_mmap(28); var k: i64 = 0
37 if m == 0 { t[0] = 48 as u8; k = 1 }
38 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
39 var i: i64 = 0; while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 }
40 return o + k
41}
42func 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 }
43func mg_mkdir(path: *u8) -> i64 { return sys_mkdir(path, 0x1ed) }
44func mg_write(path: *u8, content: *u8) -> i64 {
45 let fd: i64 = sys_openat_wr(path, 0x1a4)
46 if fd < 0 { return 0 - 1 }
47 var n: i64 = 0; while content[n] != 0 as u8 { n = n + 1 }
48 sys_write(fd, content, n); sys_close(fd); return 0
49}
50func mg_count(store: *u8) -> i64 {
51 let h: *i64 = ncfg_open(store)
52 if (h as i64) == 0 { return 0 }
53 return ncfg_count(h, "insight\x00" as *u8)
54}
55func mg_assert(label: *u8, cond: i64) -> i64 {
56 if cond != 0 { mg_puts(" [PASS] "); mg_puts(label); mg_puts("\n"); return 1 }
57 mg_puts(" [FAIL] "); mg_puts(label); mg_puts("\n"); return 0
58}
59
60func main() -> i64 {
61 mg_puts("=== nx_teacher_merge_gate: multi-source unify + cross-source dedup + idempotency (liar-killed) ===\n")
62 let epoch: i64 = sys_now_realtime_sec()
63 let root: *u8 = sys_mmap(512)
64 var ro: i64 = mg_cat(root, 0, "/tmp/tmg-" as *u8); ro = mg_catn(root, ro, epoch); root[ro] = 0 as u8
65 mg_mkdir(root)
66 let A: *u8 = sys_mmap(512); mg_join(A, root, "/a.tsv\x00" as *u8)
67 let B: *u8 = sys_mmap(512); mg_join(B, root, "/b.tsv\x00" as *u8)
68 let C: *u8 = sys_mmap(512); mg_join(C, root, "/c.tsv\x00" as *u8)
69 let store: *u8 = sys_mmap(512); mg_join(store, root, "/store-\x00" as *u8)
70
71 // ledger A: rule @ col 2 (id<TAB>class<TAB>rule<TAB>x)
72 mg_write(A, "# A\nA1\tCHEAT\talpha one\tx\nA2\tPROC\talpha two\tx\nA3\tDRY\talpha three\tx\n\x00" as *u8)
73 // ledger B: rule @ col 1 (id<TAB>rule) -- different schema
74 mg_write(B, "# B\nB1\tbeta one\nB2\tbeta two\nB3\tbeta three\n\x00" as *u8)
75 // ledger C: rule @ col 2; C1 duplicates A2's rule, C2 is genuinely new
76 mg_write(C, "# C\nC1\tCLS\talpha two\tx\nC2\tCLS\tgamma new\tx\n\x00" as *u8)
77
78 var pass: i64 = 0
79 var total: i64 = 0
80 var c: i64 = 0
81
82 let addA: i64 = tsyn_merge_cols(A, "srcA\x00" as *u8, store, 0, 2)
83 c = 0; if addA == 3 { if mg_count(store) == 3 { c = 1 } }
84 total = total + 1; pass = pass + mg_assert("T1 merge A (rule@2) -> +3, count 3", c)
85
86 let addB: i64 = tsyn_merge_cols(B, "srcB\x00" as *u8, store, 0, 1)
87 c = 0; if addB == 3 { if mg_count(store) == 6 { c = 1 } }
88 total = total + 1; pass = pass + mg_assert("T2 merge B (rule@1, diff schema) -> +3, count 6 (no clobber)", c)
89
90 let oa: *u8 = sys_mmap(256)
91 c = 0; if tsyn_recall(store, "A2\x00" as *u8, oa) == 1 { if tsyn_streq(oa, "alpha two\x00" as *u8) == 1 { c = 1 } }
92 total = total + 1; pass = pass + mg_assert("T3 recall A2 from unified store == faithful", c)
93
94 let ob: *u8 = sys_mmap(256)
95 c = 0; if tsyn_recall(store, "B2\x00" as *u8, ob) == 1 { if tsyn_streq(ob, "beta two\x00" as *u8) == 1 { c = 1 } }
96 total = total + 1; pass = pass + mg_assert("T4 recall B2 from unified store == faithful", c)
97
98 c = 0; if tsyn_redundant_col(A, store, 2) == 1 { c = 1 }
99 total = total + 1; pass = pass + mg_assert("T5 ledger A redundant (rule_col=2)", c)
100
101 c = 0; if tsyn_redundant_col(B, store, 1) == 1 { c = 1 }
102 total = total + 1; pass = pass + mg_assert("T6 ledger B redundant (rule_col=1)", c)
103
104 c = 0; if tsyn_redundant_col(C, store, 2) == 0 { c = 1 }
105 total = total + 1; pass = pass + mg_assert("T7[neg] ledger C NOT redundant before merge (new rule absent)", c)
106
107 let addA2: i64 = tsyn_merge_cols(A, "srcA\x00" as *u8, store, 0, 2)
108 c = 0; if addA2 == 0 { c = 1 }
109 total = total + 1; pass = pass + mg_assert("T8 re-merge A -> 0 added (cross-source dedup => idempotent)", c)
110
111 c = 0; if mg_count(store) == 6 { c = 1 }
112 total = total + 1; pass = pass + mg_assert("T9 count still 6 after idempotent re-merge", c)
113
114 let addC: i64 = tsyn_merge_cols(C, "srcC\x00" as *u8, store, 0, 2)
115 c = 0; if addC == 1 { c = 1 }
116 total = total + 1; pass = pass + mg_assert("T10 merge C -> exactly 1 added (shared rule deduped, new one kept)", c)
117
118 c = 0; if mg_count(store) == 7 { c = 1 }
119 total = total + 1; pass = pass + mg_assert("T11 count == 7", c)
120
121 c = 0; if tsyn_redundant_col(C, store, 2) == 1 { c = 1 }
122 total = total + 1; pass = pass + mg_assert("T12 ledger C now redundant (both rules recoverable)", c)
123
124 let h: *i64 = ncfg_open(store)
125 c = 0; if tsyn_store_has_rule(h, "PHANTOM never merged insight\x00" as *u8) == 0 { c = 1 }
126 total = total + 1; pass = pass + mg_assert("T13[neg] no-fabrication: a rule never merged is ABSENT", c)
127
128 mg_puts("\n---- nx_teacher_merge gate: passed "); mg_putn(pass); mg_puts(" / "); mg_putn(total); mg_puts(" ----\n")
129 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
130 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
131 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
132 let ctr__dry: *i64 = gv_ctr()
133 ctr__dry[0] = pass
134 ctr__dry[1] = total
135 let rc__dry: i64 = gv_verdict("TEACHER-MERGE-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
136 sys_exit(rc__dry)
137 return rc__dry
138}