nx_librarian_review.nx source
↩ module page · 113 lines · 6650 B
1// nx_librarian_review.nx -- ORCHESTRATION rung 1: the LIBRARIAN auto-reviews the RESEARCHER's ingested
2// material and flags what is "not up to scruff" -- WITHOUT the operator having to ask. This is the first
3// two-teams-in-conjunction workstream (operator 2026-07-10: "the researcher could work with the librarian
4// without me having to be explicit to review our already ingested material and callout to the librarian if
5// its not up to scruff ... true orchestration like an orchestra").
6//
7// RACI for the REVIEW-INGESTED-MATERIAL workstream (recorded on the seg-store, not asserted):
8// Responsible = librarian (runs the review) Accountable = librarian
9// Consulted = researcher (owns the material) Informed = operator + critic
10// The librarian's quality bar per doc (mechanically checked, integer-only):
11// (1) FOUNDATIONED -- a signed credential exists (knowledge/foundation/cred/<name>.nxpc1). #1 trust gate.
12// (2) SUBSTANTIVE -- byte length >= a data-driven floor (empty/stub docs are not knowledge).
13// (3) NOT-TRUNCATED -- length < the CID read cap (a doc AT the cap was truncated before content-address).
14// A doc failing any -> NOT-UP-TO-SCRUFF, logged + counted, and the handoff record carries the flag count so
15// the researcher (Consulted) is auto-informed to re-fetch/deepen. Read-only over the library; writes only the
16// review log + one seg-store handoff record. Composes nothing it shouldn't (DRY). license_tier: ORIGINAL
17import "nx_syscalls.nx"
18import "nx_seg_store.nx"
19const LR_MAGIC_65536: i64 = 65536
20
21const LR_LIBDIR: *u8 = "knowledge/library"
22const LR_CREDDIR: *u8 = "knowledge/foundation/cred"
23const LR_LOG: *u8 = "knowledge/status/librarian_review.log"
24const LR_STORE: *u8 = "knowledge/store/orchestration-"
25const LR_MINBYTES: i64 = 200 // data-driven substantive floor
26const LR_CIDCAP: i64 = 262143 // the ingest_foundation read cap (>= this => truncated)
27
28func lr_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
29func lr_cat(dst: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} dst[o]=0 as u8; return o }
30func lr_w(fd: i64, s: *u8) -> i64 { sys_write(fd, s, lr_slen(s)); return 0 }
31func lr_wn(fd: i64, v0: i64) -> i64 {
32 if v0==0 { sys_write(fd,"0" as *u8,1); return 0 }
33 var v: i64=v0; if v<0 { sys_write(fd,"-" as *u8,1); v=0-v }
34 let t: *u8=sys_mmap(24); var k: i64=0; while v>0 { t[k]=(48+(v%10)) as u8; v=v/10; k=k+1 }
35 let r: *u8=sys_mmap(24); var i: i64=0; while k>0 { k=k-1; r[i]=t[k]; i=i+1 } sys_write(fd,r,i); return 0
36}
37func lr_ends(name: *u8, suf: *u8) -> i64 {
38 let nl: i64=lr_slen(name); let sl: i64=lr_slen(suf); if sl>nl {return 0}
39 var i: i64=0; while i<sl { if name[nl-sl+i]!=suf[i]{return 0} i=i+1 } return 1
40}
41// file byte length via open+lseek-END; -1 if absent.
42func lr_fsize(path: *u8) -> i64 {
43 let fd: i64 = sys_openat_rd(path); if fd<0 { return 0-1 }
44 let sz: i64 = sys_lseek(fd, 0, 2); sys_close(fd); return sz
45}
46func lr_exists(path: *u8) -> i64 {
47 let fd: i64 = sys_openat_rd(path); if fd<0 { return 0 } sys_close(fd); return 1
48}
49
50// review ONE doc. counts: [0]=reviewed [1]=up-to-scruff [2]=not-foundationed [3]=too-thin [4]=truncated.
51// returns 1 up-to-scruff / 0 flagged. logfd -1 = console only.
52func lr_one(name: *u8, counts: *i64, logfd: i64) -> i64 {
53 counts[0] = counts[0] + 1
54 let lib: *u8 = sys_mmap(512); var o: i64 = lr_cat(lib, 0, LR_LIBDIR); lib[o]=47 as u8; o=o+1; lr_cat(lib, o, name)
55 let cred: *u8 = sys_mmap(512); o = lr_cat(cred, 0, LR_CREDDIR); cred[o]=47 as u8; o=o+1; o=lr_cat(cred, o, name); lr_cat(cred, o, ".nxpc1" as *u8)
56 let sz: i64 = lr_fsize(lib)
57 let foundationed: i64 = lr_exists(cred)
58 var ok: i64 = 1
59 var reason: *u8 = "ok\x00" as *u8
60 if foundationed == 0 { ok=0; counts[2]=counts[2]+1; reason="not-foundationed(no-credential)" as *u8 }
61 else { if sz < LR_MINBYTES { ok=0; counts[3]=counts[3]+1; reason="too-thin(stub)" as *u8 }
62 else { if sz >= LR_CIDCAP { ok=0; counts[4]=counts[4]+1; reason="truncated-at-cid-cap" as *u8 } } }
63 if ok == 1 { counts[1]=counts[1]+1 } else {
64 lr_w(logfd, "REVIEW-FLAG "); lr_w(logfd, name); lr_w(logfd, " bytes="); lr_wn(logfd, sz)
65 lr_w(logfd, " NOT-UP-TO-SCRUFF reason="); lr_w(logfd, reason); lr_w(logfd, "\n")
66 }
67 return ok
68}
69
70// record the RACI handoff on the seg-store so orchestration state is durable, not asserted.
71func lr_handoff(reviewed: i64, scruff: i64, flagged: i64) -> i64 {
72 let val: *u8 = sys_mmap(512)
73 var o: i64 = 0
74 o = lr_cat(val, o, "workstream=review-ingested-material|R=librarian|A=librarian|C=researcher|I=operator,critic|reviewed=" as *u8)
75 let t: *u8 = sys_mmap(24); var v: i64=reviewed; var k: i64=0; if v==0{t[0]=48 as u8;k=1}; while v>0{t[k]=(48+(v%10)) as u8;v=v/10;k=k+1}
76 while k>0 { k=k-1; val[o]=t[k]; o=o+1 }
77 o = lr_cat(val, o, "|scruff=" as *u8); v=scruff; k=0; if v==0{t[0]=48 as u8;k=1}; while v>0{t[k]=(48+(v%10)) as u8;v=v/10;k=k+1}; while k>0{k=k-1;val[o]=t[k];o=o+1}
78 o = lr_cat(val, o, "|flagged=" as *u8); v=flagged; k=0; if v==0{t[0]=48 as u8;k=1}; while v>0{t[k]=(48+(v%10)) as u8;v=v/10;k=k+1}; while k>0{k=k-1;val[o]=t[k];o=o+1}
79 var verdict: *u8 = "|verdict=CATALOG-CLEAN" as *u8
80 if flagged > 0 { verdict = "|verdict=RESEARCHER-ACTION-REQUESTED" as *u8 }
81 o = lr_cat(val, o, verdict)
82 val[o] = 0 as u8
83 let w: *i64 = ss_begin()
84 if ss_add(w, 1, "orch:handoff:librarian-review" as *u8, val, o) != 0 { return 0-1 }
85 if ss_commit(LR_STORE, w, sys_now_us()) != 0 { return 0-2 }
86 return 0
87}
88
89// walk the library, review every *.txt, log+count, record the handoff. fills counts[0..5]. returns reviewed.
90func lr_run(counts: *i64) -> i64 {
91 let logfd: i64 = sys_openat_wr(LR_LOG, 0x1a4)
92 let fd: i64 = sys_openat_rd(LR_LIBDIR)
93 if fd < 0 { return 0 }
94 let gbuf: *u8 = sys_mmap(LR_MAGIC_65536)
95 var nread: i64 = sys_getdents64(fd, gbuf, LR_MAGIC_65536)
96 while nread > 0 {
97 var off: i64 = 0
98 while off < nread {
99 let rl: *u8 = ((gbuf as i64)+off+16) as *u8
100 let reclen: i64 = (rl[0] as i64) + ((rl[1] as i64) * 256)
101 if reclen <= 0 { off = nread } else {
102 let name: *u8 = ((gbuf as i64)+off+19) as *u8
103 if lr_ends(name, ".txt" as *u8) == 1 { lr_one(name, counts, logfd) }
104 off = off + reclen
105 }
106 }
107 nread = sys_getdents64(fd, gbuf, LR_MAGIC_65536)
108 }
109 sys_close(fd)
110 if logfd >= 0 { sys_close(logfd) }
111 lr_handoff(counts[0], counts[1], counts[0] - counts[1])
112 return counts[0]
113}