code wiki / (root) / nx_teacher_synthesize.nx

nx_teacher_synthesize.nx source

↩ module page · 196 lines · 9134 B

1// nx_teacher_synthesize.nx -- the SYNTHESIS bridge of the Nishi Teacher/Trainer (Teacher arc, rung R2). 2// 3// Operator doctrine (2026-06-20): "we want to CONSUME all our information FROM the nishi teacher and 4// trainer so we get the insights to improve the nishi team's capabilities -- and once the information is 5// SYNTHESIZED it is REDUNDANT and we can move away from [the raw source]." So a raw insight-bearing 6// artifact (e.g. a quarantined ledger like build_learnings.tsv) is not discarded as junk: its insights 7// are first INGESTED into the team's living capability store, where the team consumes them; the raw 8// source is then PROVABLY redundant (every insight recoverable from the store) -> safe to retire. 9// 10// This organ ingests an insight LEDGER (tab-delimited `id<TAB>class<TAB>learning<TAB>reinforces`, with 11// `#` comment lines) and synthesizes each row into the NATIVE content-addressed seg_store (ncfg / 12// nx_native_config -- NOT a borrowed TSV, the operator's "native ecosystem only" mandate) under tag 13// "insight", at a fixed team-capability prefix. Three verbs: 14// tsyn_ingest_ledger(src,source_tag,prefix) -> count synthesized (-1 read fail, -2 commit fail) 15// tsyn_recall(prefix,id,out) -> 1 if the team can CONSUME that insight by id from the store 16// tsyn_redundant(src,prefix) -> 1 iff EVERY learning in src is recoverable from the store 17// (the source is fully absorbed = redundant = retirable) 18// Parameterized so the gate targets /tmp fixtures; main() runs the real quarantined ledger. 19// No hardware writes, no network (Rule 26). Faithful extraction only -- no fabricated insight (Rule 4). 20// license_tier: ORIGINAL 21import "nx_syscalls.nx" 22import "nx_native_config.nx" 23const K_MAGIC_4096: i64 = 4096 24const K_MAGIC_4095: i64 = 4095 25 26func tsyn_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 } 27func tsyn_putn(v: i64) -> i64 { 28 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 29 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 30 let d: *u8 = sys_mmap(24); var k: i64 = 0 31 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 32 var j: i64 = k - 1 33 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 34 return 0 35} 36func tsyn_streq(a: *u8, b: *u8) -> i64 { 37 var i: i64 = 0 38 while a[i] != 0 as u8 { if a[i] != b[i] { return 0 } i = i + 1 } 39 if b[i] != 0 as u8 { return 0 } 40 return 1 41} 42// extract tab-field f (0-based) of line bytes [ls,le) into a fresh NUL-terminated buffer. 43func tsyn_field(b: *u8, ls: i64, le: i64, f: i64) -> *u8 { 44 var cur: i64 = 0 45 var p: i64 = ls 46 while cur < f { 47 if p >= le { let e: *u8 = sys_mmap(2); e[0] = 0 as u8; return e } 48 if b[p] == 9 as u8 { cur = cur + 1 } 49 p = p + 1 50 } 51 let out: *u8 = sys_mmap(K_MAGIC_4096); var o: i64 = 0 52 while p < le { 53 if b[p] == 9 as u8 { p = le } else { if o < K_MAGIC_4095 { out[o] = b[p]; o = o + 1 } p = p + 1 } 54 } 55 out[o] = 0 as u8 56 return out 57} 58// author one synthesized insight record (source, id, class, rule, reinforces) into the native store. 59func tsyn_row(w: *i64, idx: i64, src: *u8, id: *u8, cls: *u8, rule: *u8, reinf: *u8) -> i64 { 60 let keys: *i64 = sys_mmap(8 * 8) as *i64 61 let vals: *i64 = sys_mmap(8 * 8) as *i64 62 keys[0] = ("source\x00") as i64; vals[0] = (src as i64) 63 keys[1] = ("id\x00") as i64; vals[1] = (id as i64) 64 keys[2] = ("class\x00") as i64; vals[2] = (cls as i64) 65 keys[3] = ("rule\x00") as i64; vals[3] = (rule as i64) 66 keys[4] = ("reinforces\x00") as i64; vals[4] = (reinf as i64) 67 return ncfg_add_row(w, "insight\x00" as *u8, idx, keys, vals, 5) 68} 69// ingest a raw insight-ledger -> synthesize every data row into the native store. returns count, or <0. 70func tsyn_ingest_ledger(src_path: *u8, source_tag: *u8, store_prefix: *u8) -> i64 { 71 let szp: *i64 = sys_mmap(16) as *i64 72 let body: *u8 = sys_read_file(src_path, szp) 73 if (body as i64) == 0 { return 0 - 1 } 74 let n: i64 = szp[0] 75 let w: *i64 = ncfg_begin() 76 var idx: i64 = 0 77 var ls: i64 = 0 78 var i: i64 = 0 79 while i <= n { 80 var atend: i64 = 0 81 if i == n { atend = 1 } 82 if i < n { if body[i] == 10 as u8 { atend = 1 } } 83 if atend == 1 { 84 if i > ls { if body[ls] != 35 as u8 { 85 let id: *u8 = tsyn_field(body, ls, i, 0) 86 let learning: *u8 = tsyn_field(body, ls, i, 2) 87 if id[0] != (0 as u8) { if learning[0] != (0 as u8) { 88 let cls: *u8 = tsyn_field(body, ls, i, 1) 89 let reinf: *u8 = tsyn_field(body, ls, i, 3) 90 tsyn_row(w, idx, source_tag, id, cls, learning, reinf) 91 idx = idx + 1 92 } } 93 } } 94 ls = i + 1 95 } 96 i = i + 1 97 } 98 ncfg_set_count(w, "insight\x00" as *u8, idx) 99 if ncfg_commit(store_prefix, w) != 0 { return 0 - 2 } 100 return idx 101} 102// CONSUME-from-teacher: copy the rule of the insight whose id==want into out (NUL-term). 1=found. 103func tsyn_recall(store_prefix: *u8, want_id: *u8, out: *u8) -> i64 { 104 let h: *i64 = ncfg_open(store_prefix) 105 if (h as i64) == 0 { out[0] = 0 as u8; return 0 } 106 let cnt: i64 = ncfg_count(h, "insight\x00" as *u8) 107 let rk: *i64 = sys_mmap(8 * 16) as *i64 108 let rv: *i64 = sys_mmap(8 * 16) as *i64 109 var i: i64 = 0 110 while i < cnt { 111 let rf: i64 = ncfg_row(h, "insight\x00" as *u8, i, rk, rv, 16) 112 if rf > 0 { 113 let idb: *u8 = ncfg_field(rk, rv, rf, "id\x00" as *u8) 114 if (idb as i64) != 0 { if tsyn_streq(idb, want_id) == 1 { 115 let rb: *u8 = ncfg_field(rk, rv, rf, "rule\x00" as *u8) 116 var j: i64 = 0 117 if (rb as i64) != 0 { while rb[j] != (0 as u8) { out[j] = rb[j]; j = j + 1 } } 118 out[j] = 0 as u8 119 return 1 120 } } 121 } 122 i = i + 1 123 } 124 out[0] = 0 as u8 125 return 0 126} 127// is some insight in the store whose rule == want? (used by the redundancy proof) 128func tsyn_store_has_rule(h: *i64, want: *u8) -> i64 { 129 let cnt: i64 = ncfg_count(h, "insight\x00" as *u8) 130 let rk: *i64 = sys_mmap(8 * 16) as *i64 131 let rv: *i64 = sys_mmap(8 * 16) as *i64 132 var i: i64 = 0 133 while i < cnt { 134 let rf: i64 = ncfg_row(h, "insight\x00" as *u8, i, rk, rv, 16) 135 if rf > 0 { 136 let rb: *u8 = ncfg_field(rk, rv, rf, "rule\x00" as *u8) 137 if (rb as i64) != 0 { if tsyn_streq(rb, want) == 1 { return 1 } } 138 } 139 i = i + 1 140 } 141 return 0 142} 143// REDUNDANCY PROOF: 1 iff EVERY learning in the raw source is recoverable from the store. 144func tsyn_redundant(src_path: *u8, store_prefix: *u8) -> i64 { 145 let h: *i64 = ncfg_open(store_prefix) 146 if (h as i64) == 0 { return 0 } 147 let szp: *i64 = sys_mmap(16) as *i64 148 let body: *u8 = sys_read_file(src_path, szp) 149 if (body as i64) == 0 { return 0 } 150 let n: i64 = szp[0] 151 var allok: i64 = 1 152 var ls: i64 = 0 153 var i: i64 = 0 154 while i <= n { 155 var atend: i64 = 0 156 if i == n { atend = 1 } 157 if i < n { if body[i] == 10 as u8 { atend = 1 } } 158 if atend == 1 { 159 if i > ls { if body[ls] != 35 as u8 { 160 let learning: *u8 = tsyn_field(body, ls, i, 2) 161 if learning[0] != (0 as u8) { if tsyn_store_has_rule(h, learning) == 0 { allok = 0 } } 162 } } 163 ls = i + 1 164 } 165 i = i + 1 166 } 167 return allok 168} 169 170func main() -> i64 { 171 tsyn_puts("=== Nishi Teacher SYNTHESIZE: consume a raw insight-ledger -> native team-capability store ===\n") 172 let src: *u8 = "knowledge/_quarantine/registry/build_learnings.tsv\x00" as *u8 173 let store: *u8 = "knowledge/store/teacher-insights-\x00" as *u8 174 let cnt: i64 = tsyn_ingest_ledger(src, "build_learnings\x00" as *u8, store) 175 if cnt < 0 { 176 tsyn_puts(" FATAL: cannot read/commit the source ledger (rc="); tsyn_putn(cnt); tsyn_puts(")\n") 177 sys_exit(1); return 1 178 } 179 tsyn_puts(" synthesized "); tsyn_putn(cnt); tsyn_puts(" insights from build_learnings.tsv -> knowledge/store/teacher-insights-\n") 180 let red: i64 = tsyn_redundant(src, store) 181 if red == 1 { 182 tsyn_puts(" REDUNDANCY-PROOF: every insight is recoverable from the teacher store -> the raw ledger is REDUNDANT (safe to retire / already quarantined)\n") 183 } else { 184 tsyn_puts(" REDUNDANCY-PROOF: INCOMPLETE -- the raw ledger is NOT fully absorbed; do NOT retire it\n") 185 } 186 // demonstrate CONSUME-from-teacher (the team reads an insight from the store, not the raw TSV) 187 let out: *u8 = sys_mmap(K_MAGIC_4096) 188 if tsyn_recall(store, "BL-001\x00" as *u8, out) == 1 { 189 tsyn_puts(" consume BL-001 from teacher store: ") 190 var k: i64 = 0 191 while out[k] != (0 as u8) { if k < 90 { sys_write(1, (out as i64 + k) as *u8, 1) } k = k + 1 } 192 tsyn_puts(" ...\n") 193 } 194 if red == 1 { sys_exit(0); return 0 } 195 sys_exit(2); return 2 196}