nx_teacher_merge.nx source
↩ module page · 142 lines · 7265 B
1// nx_teacher_merge.nx -- MULTI-SOURCE synthesis into ONE unified Nishi Teacher capability store
2// (Teacher arc, rung R2b). Extends nx_teacher_synthesize (DRY -- reuses tsyn_field/streq/store_has_rule/
3// recall) so the team can "consume ALL our information" from a single native store, not many raw TSVs.
4//
5// Operator doctrine [[feedback-synthesize-before-retire]]: synthesize each information-bearing ledger's
6// insight into the team's living capability store; once absorbed the raw is redundant. This rung makes
7// the store MULTI-SOURCE + heterogeneous-schema + de-duplicated:
8// tsyn_merge_cols(src, source_tag, prefix, id_col, rule_col) -> rows ADDED (-1 read, -2 commit)
9// appends the ledger's insights at the store's CURRENT count offset (so a 2nd source never clobbers
10// the 1st -- seg_store append-segment + last-wins count), reading the id + the insight from the
11// caller-specified columns (so build_learnings 0/2, landmines 0/2, nishi_cardinals 0/1 all map in),
12// and SKIPS any rule already in the store (cross-source dedup -> idempotent re-merge).
13// tsyn_redundant_col(src, prefix, rule_col) -> 1 iff every insight of src is recoverable from the store.
14// Faithful extraction only -- no fabricated insight (Rule 4). No hardware writes / network (Rule 26).
15// license_tier: ORIGINAL
16import "nx_teacher_synthesize.nx"
17import "nx_syscalls.nx"
18const K_MAGIC_4096: i64 = 4096
19
20// author one unified insight record {source, id, rule} (the consumable essence) into the native store.
21func tsyn_row3(w: *i64, idx: i64, src: *u8, id: *u8, rule: *u8) -> i64 {
22 let keys: *i64 = sys_mmap(8 * 8) as *i64
23 let vals: *i64 = sys_mmap(8 * 8) as *i64
24 keys[0] = ("source\x00") as i64; vals[0] = (src as i64)
25 keys[1] = ("id\x00") as i64; vals[1] = (id as i64)
26 keys[2] = ("rule\x00") as i64; vals[2] = (rule as i64)
27 return ncfg_add_row(w, "insight\x00" as *u8, idx, keys, vals, 3)
28}
29// merge one ledger into the unified store at the current offset, with cross-source dedup.
30func tsyn_merge_cols(src_path: *u8, source_tag: *u8, store_prefix: *u8, id_col: i64, rule_col: i64) -> i64 {
31 let szp: *i64 = sys_mmap(16) as *i64
32 let body: *u8 = sys_read_file(src_path, szp)
33 if (body as i64) == 0 { return 0 - 1 }
34 let n: i64 = szp[0]
35 let h0: *i64 = ncfg_open(store_prefix) // pre-merge snapshot: offset + dedup source
36 var base: i64 = 0
37 if (h0 as i64) != 0 { base = ncfg_count(h0, "insight\x00" as *u8) }
38 let w: *i64 = ncfg_begin()
39 var added: i64 = 0
40 var ls: i64 = 0
41 var i: i64 = 0
42 while i <= n {
43 var atend: i64 = 0
44 if i == n { atend = 1 }
45 if i < n { if body[i] == 10 as u8 { atend = 1 } }
46 if atend == 1 {
47 if i > ls { if body[ls] != 35 as u8 {
48 let rule: *u8 = tsyn_field(body, ls, i, rule_col)
49 if rule[0] != (0 as u8) {
50 var dup: i64 = 0
51 if (h0 as i64) != 0 { if tsyn_store_has_rule(h0, rule) == 1 { dup = 1 } }
52 if dup == 0 {
53 let id: *u8 = tsyn_field(body, ls, i, id_col)
54 tsyn_row3(w, base + added, source_tag, id, rule)
55 added = added + 1
56 }
57 }
58 } }
59 ls = i + 1
60 }
61 i = i + 1
62 }
63 if added == 0 { return 0 } // nothing new -> no junk segment, idempotent
64 ncfg_set_count(w, "insight\x00" as *u8, base + added)
65 if ncfg_commit(store_prefix, w) != 0 { return 0 - 2 }
66 return added
67}
68// REDUNDANCY PROOF for a ledger whose insight lives in rule_col.
69func tsyn_redundant_col(src_path: *u8, store_prefix: *u8, rule_col: i64) -> i64 {
70 let h: *i64 = ncfg_open(store_prefix)
71 if (h as i64) == 0 { return 0 }
72 let szp: *i64 = sys_mmap(16) as *i64
73 let body: *u8 = sys_read_file(src_path, szp)
74 if (body as i64) == 0 { return 0 }
75 let n: i64 = szp[0]
76 var allok: i64 = 1
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 rule: *u8 = tsyn_field(body, ls, i, rule_col)
86 if rule[0] != (0 as u8) { if tsyn_store_has_rule(h, rule) == 0 { allok = 0 } }
87 } }
88 ls = i + 1
89 }
90 i = i + 1
91 }
92 return allok
93}
94// add ONE insight directly to the unified store (NATIVE, no TSV) at offset, with cross-source dedup.
95// returns 1 added, 0 dup, -2 commit-fail. The autonomous-learning path: the team captures a fresh lesson
96// from its OWN work and it is immediately consumable (recall) / schedulable (trainer) / masterable (mastery).
97func tsyn_add_insight(store_prefix: *u8, source_tag: *u8, id: *u8, rule: *u8) -> i64 {
98 let h0: *i64 = ncfg_open(store_prefix)
99 var base: i64 = 0
100 if (h0 as i64) != 0 {
101 base = ncfg_count(h0, "insight\x00" as *u8)
102 if tsyn_store_has_rule(h0, rule) == 1 { return 0 } // dedup (idempotent re-capture)
103 }
104 let w: *i64 = ncfg_begin()
105 tsyn_row3(w, base, source_tag, id, rule)
106 ncfg_set_count(w, "insight\x00" as *u8, base + 1)
107 if ncfg_commit(store_prefix, w) != 0 { return 0 - 2 }
108 return 1
109}
110// merge one real ledger + report (honest per-source line).
111func tsyn_merge_report(src: *u8, tag: *u8, store: *u8, idc: i64, rc: i64) -> i64 {
112 let added: i64 = tsyn_merge_cols(src, tag, store, idc, rc)
113 tsyn_puts(" + "); tsyn_puts(tag)
114 if added < 0 { tsyn_puts(": SKIP (unreadable rc="); tsyn_putn(added); tsyn_puts(")\n"); return 0 }
115 tsyn_puts(": added "); tsyn_putn(added)
116 if tsyn_redundant_col(src, store, rc) == 1 { tsyn_puts(" (redundant-PROVEN)\n") } else { tsyn_puts(" (NOT fully absorbed!)\n") }
117 return added
118}
119
120func main() -> i64 {
121 tsyn_puts("=== Nishi Teacher MERGE: unify all our insight ledgers -> ONE native team-capability store ===\n")
122 let store: *u8 = "knowledge/store/teacher-capability-\x00" as *u8
123 // three clean insight ledgers: process lessons + technical traps + the teacher's extracted cardinals.
124 tsyn_merge_report("knowledge/_quarantine/registry/build_learnings.tsv\x00" as *u8, "build_learnings\x00" as *u8, store, 0, 2)
125 tsyn_merge_report("knowledge/registry/landmines.tsv\x00" as *u8, "landmines\x00" as *u8, store, 0, 2)
126 tsyn_merge_report("knowledge/registry/nishi_cardinals.tsv\x00" as *u8, "nishi_cardinals\x00" as *u8, store, 0, 1)
127
128 let h: *i64 = ncfg_open(store)
129 var total: i64 = 0
130 if (h as i64) != 0 { total = ncfg_count(h, "insight\x00" as *u8) }
131 tsyn_puts(" UNIFIED teacher-capability store now holds "); tsyn_putn(total); tsyn_puts(" insights (consume from knowledge/store/teacher-capability-)\n")
132
133 // demonstrate consume-from-teacher across sources (one id from each)
134 let out: *u8 = sys_mmap(K_MAGIC_4096)
135 if tsyn_recall(store, "LM-001\x00" as *u8, out) == 1 {
136 tsyn_puts(" recall landmine LM-001: ")
137 var k: i64 = 0
138 while out[k] != (0 as u8) { if k < 80 { sys_write(1, (out as i64 + k) as *u8, 1) } k = k + 1 }
139 tsyn_puts("\n")
140 }
141 sys_exit(0); return 0
142}