code wiki / (root) / nx_teacher_consult.nx

nx_teacher_consult.nx source

↩ module page · 89 lines · 4124 B

1// nx_teacher_consult.nx -- makes the unified capability store ACTIONABLE (Teacher arc, rung R4): the team 2// (or operator) ASKS "what do we know about <topic>?" and gets every matching insight. Until now the store 3// was write + recall-by-id only; consult turns 113 stored lessons into on-demand wisdom the team consults 4// WHILE working (e.g. before a build: consult "compile"; before a publish: consult "publish"). 5// 6// Match = the insight's rule CONTAINS the query (case-insensitive substring). Reads the shared native store 7// via ncfg -- coordinates THROUGH the store (SL-004), imports nx_teacher_merge for the store machinery only. 8// No network/hardware writes (Rule 26). license_tier: ORIGINAL 9import "nx_teacher_merge.nx" 10import "nx_syscalls.nx" 11 12func tc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 } 13func tc_putn(v: i64) -> i64 { 14 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 15 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 16 let d: *u8 = sys_mmap(24); var k: i64 = 0 17 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var j: i64 = k - 1 19 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 20 return 0 21} 22func tc_lc(c: i64) -> i64 { if c >= 65 { if c <= 90 { return c + 32 } } return c } 23// case-insensitive: is `needle` (NUL-term) a substring of `hay` (NUL-term)? 24func tc_contains_ci(hay: *u8, needle: *u8) -> i64 { 25 var hl: i64 = 0; while hay[hl] != (0 as u8) { hl = hl + 1 } 26 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 } 27 if nl == 0 { return 0 } 28 var i: i64 = 0 29 while i + nl <= hl { 30 var j: i64 = 0; var ok: i64 = 1 31 while j < nl { if tc_lc(hay[i+j] as i64) != tc_lc(needle[j] as i64) { ok = 0; j = nl } else { j = j + 1 } } 32 if ok == 1 { return 1 } 33 i = i + 1 34 } 35 return 0 36} 37// print every insight whose rule matches `query`; return the match count. 38func tc_consult(store: *u8, query: *u8) -> i64 { 39 let h: *i64 = ncfg_open(store) 40 if (h as i64) == 0 { return 0 } 41 let cnt: i64 = ncfg_count(h, "insight\x00" as *u8) 42 let rk: *i64 = sys_mmap(8 * 16) as *i64 43 let rv: *i64 = sys_mmap(8 * 16) as *i64 44 var found: i64 = 0 45 var i: i64 = 0 46 while i < cnt { 47 let rf: i64 = ncfg_row(h, "insight\x00" as *u8, i, rk, rv, 16) 48 if rf > 0 { 49 let rule: *u8 = ncfg_field(rk, rv, rf, "rule\x00" as *u8) 50 if (rule as i64) != 0 { if tc_contains_ci(rule, query) == 1 { 51 let src: *u8 = ncfg_field(rk, rv, rf, "source\x00" as *u8) 52 let id: *u8 = ncfg_field(rk, rv, rf, "id\x00" as *u8) 53 found = found + 1 54 tc_puts(" [") 55 if (src as i64) != 0 { tc_puts(src) } 56 tc_puts("] ") 57 if (id as i64) != 0 { tc_puts(id) } 58 tc_puts(": ") 59 var k: i64 = 0 60 while rule[k] != (0 as u8) { if k < 100 { sys_write(1, (rule as i64 + k) as *u8, 1) } k = k + 1 } 61 tc_puts("\n") 62 } } 63 } 64 i = i + 1 65 } 66 return found 67} 68func tc_ask(store: *u8, query: *u8) -> i64 { 69 tc_puts("\n CONSULT \""); tc_puts(query); tc_puts("\":\n") 70 let n: i64 = tc_consult(store, query) 71 tc_puts(" -> "); tc_putn(n); tc_puts(" insight(s)\n") 72 return n 73} 74 75func main() -> i64 { 76 tc_puts("=== Nishi Teacher CONSULT: ask the capability store -- 'what do we know about X?' ===\n") 77 let store: *u8 = "knowledge/store/teacher-capability-\x00" as *u8 78 let h: *i64 = ncfg_open(store) 79 var total: i64 = 0 80 if (h as i64) != 0 { total = ncfg_count(h, "insight\x00" as *u8) } 81 tc_puts(" capability store: "); tc_putn(total); tc_puts(" insights\n") 82 tc_ask(store, "publish\x00" as *u8) 83 tc_ask(store, "compile\x00" as *u8) 84 tc_ask(store, "audit\x00" as *u8) 85 tc_ask(store, "concurrency\x00" as *u8) 86 tc_puts("\n the team's 113 lessons are now CONSULTABLE by topic -- wisdom on demand, not just stored.\n") 87 if total > 0 { sys_exit(0); return 0 } 88 sys_exit(1); return 1 89}