code wiki / _hdl_build / nx_research_deliver.nx
nx_research_deliver.nx source
↩ module page · 97 lines · 5396 B
1// nx_research_deliver.nx -- close the DELIVERY loop for ONE section (the researcher), honestly. The
2// researcher does REAL work: a sovereign search over the no-link-rot library (nx_library) for a
3// topic, producing CID-cited findings. ONLY if real findings exist does it flip its cued task
4// PENDING->APPLIED in the roles- store (rs_row_set) with a reference to the actual result -- so
5// `delivering` becomes true because work was COMPLETED, not because a flag was toggled. Re-running
6// nx_team_harmony then shows researcher PERFORMING (0->1), and nx_critics_review's stars climb. This
7// is the template every section follows to turn ENGAGED into MUSIC. Sovereign. license_tier: ORIGINAL
8import "nx_library.nx"
9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
10import "nx_role_store.nx"
11import "nx_syscalls.nx"
12
13const RD_CHAN: *u8 = "researcherq"
14const RD_TOPIC: *u8 = "hydrogen" // a term the sovereign corpus genuinely holds (verified: 4 hits)
15
16func rd_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
18// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
19// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
20// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
21func rd_n(v: i64) -> i64 { nxi_out(v); return 0 }
22func rd_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 }
23func rd_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } return o }
24func rd_catn(dst: *u8, off: i64, v: i64) -> i64 { var o: i64 = off; if v == 0 { dst[o] = 48 as u8; return o + 1 } var m: i64 = v; let t: *u8 = sys_mmap(24); var k: i64 = 0; while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = k - 1; while i >= 0 { dst[o] = t[i]; o = o + 1; i = i - 1 } return o }
25
26// substring index of needle in buf[0..n), or -1.
27func rd_find(buf: *u8, n: i64, needle: *u8) -> i64 {
28 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
29 if nl == 0 { return 0 - 1 }
30 var i: i64 = 0
31 while i + nl <= n {
32 var j: i64 = 0; var ok: i64 = 1
33 while j < nl { if buf[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } }
34 if ok == 1 { return i }
35 i = i + 1
36 }
37 return 0 - 1
38}
39
40// REAL research: count library docs containing `topic`; capture the first matching CID. The work.
41func rd_research(topic: *u8, first_cid: *u8) -> i64 {
42 let cids: *i64 = sys_mmap(8 * 256) as *i64
43 let nc: i64 = lib_list(cids, 256)
44 let ptr: *i64 = sys_mmap(16) as *i64; let len: *i64 = sys_mmap(16) as *i64
45 var hits: i64 = 0; var first_set: i64 = 0
46 var i: i64 = 0
47 while i < nc {
48 let cid: *u8 = cids[i] as *u8
49 if lib_get(cid, ptr, len) == 1 {
50 if rd_find(ptr[0] as *u8, len[0], topic) >= 0 {
51 hits = hits + 1
52 if first_set == 0 { var c: i64 = 0; while cid[c] != (0 as u8) { if c < 47 { first_cid[c] = cid[c] } c = c + 1 } first_cid[c] = 0 as u8; first_set = 1 }
53 }
54 }
55 i = i + 1
56 }
57 return hits
58}
59
60// seq of the first PENDING row in `channel`, or -1.
61func rd_find_pending(channel: *u8) -> i64 {
62 let n: i64 = rs_count(channel)
63 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
64 let fld: *u8 = sys_mmap(64)
65 var i: i64 = 0
66 while i < n {
67 if rs_get_seq(channel, i, pq, lq) == 1 { rs_field(pq[0] as *u8, lq[0], 1, fld); if rd_streq(fld, "PENDING" as *u8) == 1 { return i } }
68 i = i + 1
69 }
70 return 0 - 1
71}
72
73func main(argc: i64, argv: *i64) -> i64 {
74 var topic: *u8 = RD_TOPIC
75 if argc >= 2 { topic = argv[1] as *u8 }
76 rd_w("=== RESEARCHER delivers: sovereign library search for \"" as *u8); rd_w(topic); rd_w("\" ===\n" as *u8)
77 let cid: *u8 = sys_mmap(64)
78 let hits: i64 = rd_research(topic, cid)
79 rd_w(" research done: " as *u8); rd_n(hits); rd_w(" CID-cited finding(s)" as *u8)
80 if hits > 0 { rd_w(" (first cite: " as *u8); rd_w(cid); rd_w(")" as *u8) }
81 rd_w("\n" as *u8)
82 if hits <= 0 { rd_w(" no findings -> nothing to deliver (honest -- no APPLIED).\n" as *u8); sys_exit(1); return 1 }
83
84 let seq: i64 = rd_find_pending(RD_CHAN)
85 if seq < 0 { rd_w(" no PENDING cue in researcherq -> run nx_harmony_conduct first.\n" as *u8); sys_exit(1); return 1 }
86
87 // record the COMPLETED work: PENDING -> APPLIED with a reference to the real result.
88 let row: *u8 = sys_mmap(256); var o: i64 = 0
89 o = rd_cat(row, o, "researcher\tAPPLIED\tdelivered:research topic=" as *u8); o = rd_cat(row, o, topic)
90 o = rd_cat(row, o, " hits=" as *u8); o = rd_catn(row, o, hits)
91 o = rd_cat(row, o, " cite=" as *u8); o = rd_cat(row, o, cid)
92 o = rd_cat(row, o, "\tby=researcher" as *u8); row[o] = 0 as u8
93 rs_row_set(RD_CHAN, seq, row, o)
94 rd_w(" DELIVERED: researcherq seq=" as *u8); rd_n(seq); rd_w(" PENDING -> APPLIED (real research recorded).\n" as *u8)
95 rd_w(" the researcher just played its first finished note. Re-judge to see PERFORMANCE 0->1.\n" as *u8)
96 sys_exit(0); return 0
97}