code wiki / (root) / nx_teacher_serve.nx

nx_teacher_serve.nx source

↩ module page · 45 lines · 3655 B

1// nx_teacher_serve.nx -- SERVE-WRAPPER making the TEACHER role's capability MCP-callable (operator 2026-07-10). Library-role 2// pattern (4th): nx_teacher is a LIBRARY (no main; nx_teacher_ingest reads FILES + appends registries = side-effecting). This 3// wrapper exercises the teacher's real STRUCTURE-VALIDATION primitives (mem_eq + sec_bounds) on SYNTHETIC in-memory lessons -- 4// NO files, NO side effects: a well-formed lesson (title + CONTEXT/TAKEAWAYS/EVIDENCE/CARDINALS/DRILLS) PASSES, one missing a 5// required section FAILS -> the teacher's S-class gate discriminates. Clean exit 0 + JSON so nishi_crew dispatches `teacher train`. 6// Import ONLY nx_teacher.nx (brings nx_syscalls). NOTE: nx_teacher already ships these '##' literals + compiles, so the '#' 7// lexer note does not bite here. JSON trick: tsv_w2 converts ' -> ". license_tier: ORIGINAL expect_exit: 0 8import "nx_teacher.nx" 9import "nx_teacher_lesson.nx" // nx_teacher_ingest / nx_teacher_emit_index -- the deleted lesson organ 10 11func tsv_raw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func tsv_w2(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){ if s[n]==(39 as u8) { let q: *u8=sys_mmap(1); q[0]=34 as u8; sys_write(1,q,1) } else { sys_write(1,(s as i64+n) as *u8,1) } n=n+1 } return 0 } 13func tsv_bit(b: i64) -> i64 { let d: *u8=sys_mmap(1); d[0]=(48+b) as u8; sys_write(1,d,1); return 0 } 14func tsv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15 16// the teacher's S-class structure gate, run on an in-memory lesson body using the library's OWN mem_eq + sec_bounds. 17func tsv_valid(body: *u8, n: i64) -> i64 { 18 let ob: *i64 = sys_mmap(16) as *i64 19 if mem_eq(body, 0, n, "# \x00" as *u8) == 0 { return 0 } 20 if sec_bounds(body, n, "## CONTEXT\x00" as *u8, ob) == 0 { return 0 } 21 if sec_bounds(body, n, "## TAKEAWAYS\x00" as *u8, ob) == 0 { return 0 } 22 if sec_bounds(body, n, "## EVIDENCE\x00" as *u8, ob) == 0 { return 0 } 23 if sec_bounds(body, n, "## CARDINALS\x00" as *u8, ob) == 0 { return 0 } 24 if sec_bounds(body, n, "## DRILLS\x00" as *u8, ob) == 0 { return 0 } 25 return 1 26} 27 28func main() -> i64 { 29 // a well-formed S-class lesson: title + all five required sections. 30 let good: *u8 = "# Desirable Difficulties\n## CONTEXT\n- spacing helps retention\n## TAKEAWAYS\n- test yourself\n## EVIDENCE\n- Bjork 1994\n## CARDINALS\n- retrieval beats rereading\n## DRILLS\n- recall three cardinals cold\n\x00" as *u8 31 // the same lesson MISSING the required ## DRILLS section -> not S-class. 32 let bad: *u8 = "# Missing Drills\n## CONTEXT\n- x\n## TAKEAWAYS\n- y\n## EVIDENCE\n- z\n## CARDINALS\n- c\n\x00" as *u8 33 let good_ok: i64 = tsv_valid(good, tsv_slen(good)) 34 let bad_ok: i64 = tsv_valid(bad, tsv_slen(bad)) 35 var passes_valid: i64=0; if good_ok==1 { passes_valid=1 } 36 var rejects_broken: i64=0; if bad_ok==0 { rejects_broken=1 } 37 var ok: i64=0; if passes_valid==1 { if rejects_broken==1 { ok=1 } } 38 tsv_w2("{'v':1,'api':'nishi-crew-teacher/v1','role':'teacher','activity':'train','capability':'S-class lesson gate -- a lesson missing any required section (CONTEXT/TAKEAWAYS/EVIDENCE/CARDINALS/DRILLS) is NOT S-class and is refused (Bjork desirable-difficulties curriculum)','selftest':{'wellformed_lesson_passes':" as *u8); tsv_bit(passes_valid) 39 tsv_w2(",'missing_section_refused':" as *u8); tsv_bit(rejects_broken) 40 tsv_w2("},'verdict':'" as *u8) 41 if ok==1 { tsv_w2("GREEN (structure gate discriminates)'}" as *u8) } else { tsv_w2("RED'}" as *u8) } 42 tsv_w2("\n" as *u8) 43 if ok==1 { sys_exit(0); return 0 } 44 sys_exit(1); return 1 45}