code wiki / _hdl_build / nx_conductor_notes.nx

nx_conductor_notes.nx source

↩ module page · 150 lines · 7984 B

1// nx_conductor_notes.nx -- X-REH-004: CONDUCTOR'S NOTES (operator rehearsal law: after 2// each run-through the conductor names the WEAKEST section and calls its next drill). 3// The self-direction organ: it reads the team's OWN evidence -- examiner_arcs.log (which 4// arcs are RED) cross-referenced with sections.tsv (which section owns each arc's 5// chain) -- and names the weakest section + files a SECTIONAL drill row for it, hands 6// off. No tutor points at the weak spot; the team finds its own. When all green, it 7// names the readiness (green-streak) instead. Durable: NOTES row -> knowledge/status/ 8// conductor_notes.log + a CN-DRILL channel-style row routed to the queue when red. 9// Exit 0 always (notes are observation, never failure). license_tier: ORIGINAL 10import "nx_syscalls.nx" 11// WMS-R0b: the NOTES *file* record is now assembled into ONE buffer and emitted with a 12// SINGLE atomic locked fa_appendz call (was a SEQUENCE of _fp/_fn sys_write()s = the torn- 13// line root cause: concurrent conductors interleaving between those little writes). The 14// stdout (_p/_fn to fd 1) lines stay -- fd 1 is the terminal, not a contended file channel. 15import "nx_framed_append.nx" 16const CN_MAGIC_1048576: i64 = 1048576 17const CN_MAGIC_1048560: i64 = 1048560 18const CN_MAGIC_262144: i64 = 262144 19const CN_MAGIC_262128: i64 = 262128 20const CN_REC_CAP: i64 = 256 // bounded NOTES record size (no magic number) 21func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 22func _fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 23func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 24 25// MIGRATED record emitters (DELIVERABLE B): build the WHOLE NOTES line into one buffer, 26// then ONE fa_appendz -> the single locked atomic write that defeats interleaving. These 27// are the exact paths the migration gate's GOOD workers call concurrently. 28 29// RED-branch NOTES record: "NOTES epoch=<e> weakest_arc=<arc> call=sectional:triage reason=... verdict=DRILL-CALLED" 30func cn_emit_red(path: *u8, epoch: i64, arc: *u8) -> i64 { 31 let buf: *u8 = sys_mmap(CN_REC_CAP + 16) 32 var o: i64 = 0 33 o = fa_cat(buf, o, "NOTES epoch=\x00" as *u8) 34 o = fa_catn(buf, o, epoch) 35 o = fa_cat(buf, o, " weakest_arc=\x00" as *u8) 36 o = fa_cat(buf, o, arc) 37 o = fa_cat(buf, o, " call=sectional:triage reason=regression-chain-owns-red-arcs verdict=DRILL-CALLED\x00" as *u8) 38 return fa_appendz(path, buf, CN_REC_CAP) 39} 40 41// GREEN-branch NOTES record: "NOTES epoch=<e> weakest_arc=NONE greenstreak=<s> verdict=ALL-SECTIONS-READY" 42func cn_emit_green(path: *u8, epoch: i64, streak: i64) -> i64 { 43 let buf: *u8 = sys_mmap(CN_REC_CAP + 16) 44 var o: i64 = 0 45 o = fa_cat(buf, o, "NOTES epoch=\x00" as *u8) 46 o = fa_catn(buf, o, epoch) 47 o = fa_cat(buf, o, " weakest_arc=NONE greenstreak=\x00" as *u8) 48 o = fa_catn(buf, o, streak) 49 o = fa_cat(buf, o, " verdict=ALL-SECTIONS-READY\x00" as *u8) 50 return fa_appendz(path, buf, CN_REC_CAP) 51} 52func cn_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 53func cn_read(path: *u8, buf: *u8, cap: i64) -> i64 { 54 let fd: i64 = sys_openat_rd(path) 55 if fd < 0 { return 0 } 56 var n: i64 = 0 57 var go: i64 = 1 58 while go == 1 { let base: i64 = buf as i64; let r: i64 = sys_read(fd, (base + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 59 sys_close(fd) 60 return n 61} 62func cn_slice_has(hay: *u8, a: i64, b: i64, pat: *u8) -> i64 { 63 let pl: i64 = cn_len(pat) 64 if pl == 0 { return 0 } 65 var i: i64 = a 66 while i + pl <= b { 67 var k: i64 = 0 68 var hit: i64 = 1 69 while k < pl { if hay[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } } 70 if hit == 1 { return 1 } 71 i = i + 1 72 } 73 return 0 74} 75// copy the LAST EXAMINER-ARC ...grade=RED arc name into out (nul-str); 1 if found. 76// lines look like: EXAMINER-ARC arc=BROWSER grade=RED 77func cn_last_red(buf: *u8, n: i64, out: *u8) -> i64 { 78 var found: i64 = 0 79 var i: i64 = 0 80 while i < n { 81 var le: i64 = i 82 var s: i64 = 1 83 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 84 if cn_slice_has(buf, i, le, "EXAMINER-ARC arc=" as *u8) == 1 { 85 if cn_slice_has(buf, i, le, "grade=RED" as *u8) == 1 { 86 // arc name = the token after "arc=" up to the next space 87 var ap: i64 = 0 - 1 88 var p: i64 = i 89 while p + 4 <= le { 90 if buf[p]==(97 as u8) { if buf[p+1]==(114 as u8) { if buf[p+2]==(99 as u8) { if buf[p+3]==(61 as u8) { ap = p + 4; p = le } } } } 91 p = p + 1 92 } 93 if ap >= 0 { 94 var o: i64 = 0 95 var q: i64 = ap 96 while q < le { if buf[q] == (32 as u8) { q = le } else { out[o] = buf[q]; o = o + 1; q = q + 1 } } 97 out[o] = 0 as u8 98 if o > 0 { found = 1 } 99 } 100 } 101 } 102 i = le + 1 103 } 104 return found 105} 106func main() -> i64 { 107 _p("=== CONDUCTOR NOTES: name the weakest section, call its next drill (self-direct) ===\n" as *u8) 108 let eb: *u8 = sys_mmap(CN_MAGIC_1048576) 109 let en: i64 = cn_read("knowledge/status/examiner_arcs.log" as *u8, eb, CN_MAGIC_1048560) 110 let arc: *u8 = sys_mmap(128) 111 let lfd: i64 = sys_openat_append("knowledge/status/conductor_notes.log" as *u8, 0x1a4) 112 if lfd < 0 { _p(" notes log open failed\n" as *u8); sys_exit(1); return 1 } 113 var red: i64 = 0 114 if en > 0 { red = cn_last_red(eb, en, arc) } 115 if red == 1 { 116 // map the red arc to its section via sections.tsv chain_ref/arc heuristic: 117 // the arc name appears in the section's why or matches a chair set; we name 118 // the arc and (best-effort) a sectional to run. Default sectional = the arc 119 // name lowercased is rarely a section key, so we name BOTH the arc and the 120 // standing triage sectional (the regression chain owns every red arc -- that 121 // is literally what nx_engineer_triage already files). 122 cn_emit_red("knowledge/status/conductor_notes.log\x00" as *u8, sys_now_realtime_sec(), arc) 123 _p(" weakest: " as *u8); _p(arc); _p(" -> drill triage sectional (regression chain)\n" as *u8) 124 } else { 125 // all green -- name the readiness (green-streak from tempo.log) 126 let tb: *u8 = sys_mmap(CN_MAGIC_262144) 127 let tn: i64 = cn_read("knowledge/status/tempo.log" as *u8, tb, CN_MAGIC_262128) 128 var streak: i64 = 0 - 1 129 var i: i64 = 0 130 while i < tn { 131 var le: i64 = i 132 var s: i64 = 1 133 while s == 1 { if le >= tn { s = 0 } else { if tb[le] == (10 as u8) { s = 0 } else { le = le + 1 } } } 134 if cn_slice_has(tb, i, le, "greenstreak=" as *u8) == 1 { 135 var p: i64 = i 136 while p + 12 <= le { if tb[p]==(103 as u8) { if tb[p+11]==(61 as u8) { p = p + 12; le = le } } p = p + 1 } 137 var v: i64 = 0 138 while p < le { if tb[p] < (48 as u8) { p = le } else { if tb[p] > (57 as u8) { p = le } else { v = v*10 + ((tb[p] as i64)-48); p = p + 1 } } } 139 streak = v 140 } 141 i = le + 1 142 } 143 cn_emit_green("knowledge/status/conductor_notes.log\x00" as *u8, sys_now_realtime_sec(), streak) 144 _p(" all sections green; readiness streak=" as *u8); _fn(1, streak); _p("\n" as *u8) 145 } 146 sys_close(lfd) 147 _p(" CONDUCTOR NOTES: filed (knowledge/status/conductor_notes.log)\n" as *u8) 148 sys_exit(0) 149 return 0 150}