code wiki / _hdl_build / nx_engineer_triage.nx
nx_engineer_triage.nx source
↩ module page · 238 lines · 11164 B
1// nx_engineer_triage.nx -- the ENGINEER's regression-to-defect-row organ (PURE-NISHI
2// loop: the TEAM triages its own red pulse -- the tutor stops bash-prompting solutions).
3// ONE command, sub-10s. PLAIN LANGUAGE: when the team's health check goes red, this
4// tool reads the same scoreboard the Examiner grades, finds WHICH test rows failed,
5// copies their exact failure lines as evidence, and files ONE work order for the
6// Doctor. Nobody greps logs by hand; the next person sees a named, evidenced task.
7// 1. reads knowledge/status/examiner_arcs.conf (the Examiner's own arc table -- zero
8// duplicated knowledge; add an arc there = the Engineer triages it too)
9// 2. re-derives each arc's grade from its gate log's LAST anchor line (same doctrine)
10// 3. for every RED or ABSENT arc: drills the SAME log for row-level failures (lines
11// holding the anchor + verdict=FAIL; the last <=4 copied verbatim as ENGINEER-ROW
12// evidence -- exit codes included, the Doctor's input, never re-derived by a human)
13// 4. files ONE idempotent Doctor assignment: id=ENG-<arc> w=8 owner=Doctor TODO
14// (an existing id of ANY status is never re-filed; reconcile owns the lifecycle)
15// Durable: ENGINEER-ROW + ENGINEER-TRIAGE lines -> knowledge/status/engineer_triage.log.
16// Exit 0 = triage duty DONE (clean, or defects filed+named); exit 1 = the organ itself
17// could not read its world (conf/queue/log) -- fail loud, never silently green.
18// RACI: Engineer FINDS+FILES, Doctor root-causes, Builder ships the ten-second probe,
19// the pulse re-proves. argv[1]=arcs-conf argv[2]=queue argv[3]=triage-log (gates: scratch).
20// license_tier: ORIGINAL
21import "nx_syscalls.nx"
22const K_MAGIC_65536: i64 = 65536
23const K_MAGIC_65520: i64 = 65520
24const K_MAGIC_1048576: i64 = 1048576
25const K_MAGIC_1048560: i64 = 1048560
26const K_MAGIC_16384: i64 = 16384
27const K_MAGIC_1048592: i64 = 1048592
28func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
29func _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 }
30func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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 }
31func et_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
32func et_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i }
33func et_tab(fd: i64) -> i64 { let b: *u8 = sys_mmap(4); b[0] = 9 as u8; sys_write(fd, b, 1); return 0 }
34func et_read_all(path: *u8, buf: *u8, cap: i64) -> i64 {
35 let fd: i64 = sys_openat_rd(path)
36 if fd < 0 { return 0 }
37 var n: i64 = 0
38 var go: i64 = 1
39 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 } }
40 sys_close(fd)
41 return n
42}
43func et_slice_contains(hay: *u8, a: i64, b: i64, pat: *u8) -> i64 {
44 let pl: i64 = et_len(pat)
45 if pl == 0 { return 0 }
46 var i: i64 = a
47 while i + pl <= b {
48 var k: i64 = 0
49 var hit: i64 = 1
50 while k < pl { if hay[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
51 if hit == 1 { return 1 }
52 i = i + 1
53 }
54 return 0
55}
56func et_last_line_with(hay: *u8, n: i64, pat: *u8, out: *i64) -> i64 {
57 var found: i64 = 0
58 var i: i64 = 0
59 while i < n {
60 var le: i64 = i
61 var scan: i64 = 1
62 while scan == 1 {
63 if le >= n { scan = 0 }
64 else { if hay[le] == (10 as u8) { scan = 0 } else { le = le + 1 } }
65 }
66 if et_slice_contains(hay, i, le, pat) == 1 { out[0] = i; out[1] = le; found = 1 }
67 i = le + 1
68 }
69 return found
70}
71func et_token(conf: *u8, pos: i64, le: i64, pool: *u8, poff: *i64) -> i64 {
72 var t: i64 = pos
73 var s: i64 = 1
74 while s == 1 {
75 if t >= le { s = 0 }
76 else { if conf[t] == (32 as u8) { s = 0 } else { pool[poff[0]] = conf[t]; poff[0] = poff[0] + 1; t = t + 1 } }
77 }
78 pool[poff[0]] = 0 as u8; poff[0] = poff[0] + 1
79 return t
80}
81// is ids already a row id in the queue? (line start + the id bytes + a TAB)
82func et_id_in_queue(q: *u8, qn: i64, ids: *u8) -> i64 {
83 let il: i64 = et_len(ids)
84 var i: i64 = 0
85 var at_start: i64 = 1
86 while i < qn {
87 if at_start == 1 {
88 if i + il < qn {
89 var k: i64 = 0
90 var hit: i64 = 1
91 while k < il { if q[i+k] != ids[k] { hit = 0; k = il } else { k = k + 1 } }
92 if hit == 1 { if q[i+il] == (9 as u8) { return 1 } }
93 }
94 }
95 if q[i] == (10 as u8) { at_start = 1 } else { at_start = 0 }
96 i = i + 1
97 }
98 return 0
99}
100// drill the gate log: copy the LAST <=4 row-level failure lines (anchor + verdict=FAIL)
101// verbatim into the triage log as ENGINEER-ROW evidence; returns total failure-line count
102func et_drill(lb: *u8, ln: i64, anchor: *u8, lfd: i64) -> i64 {
103 let fp: *u8 = "verdict=FAIL" as *u8
104 var cnt: i64 = 0
105 var i: i64 = 0
106 while i < ln {
107 var le: i64 = i
108 var s: i64 = 1
109 while s == 1 { if le >= ln { s = 0 } else { if lb[le] == (10 as u8) { s = 0 } else { le = le + 1 } } }
110 if et_slice_contains(lb, i, le, anchor) == 1 { if et_slice_contains(lb, i, le, fp) == 1 { cnt = cnt + 1 } }
111 i = le + 1
112 }
113 var idx: i64 = 0
114 var j: i64 = 0
115 while j < ln {
116 var le2: i64 = j
117 var s2: i64 = 1
118 while s2 == 1 { if le2 >= ln { s2 = 0 } else { if lb[le2] == (10 as u8) { s2 = 0 } else { le2 = le2 + 1 } } }
119 var hit2: i64 = 0
120 if et_slice_contains(lb, j, le2, anchor) == 1 { if et_slice_contains(lb, j, le2, fp) == 1 { hit2 = 1 } }
121 if hit2 == 1 {
122 idx = idx + 1
123 if idx + 4 > cnt {
124 _fp(lfd, "ENGINEER-ROW " as *u8)
125 let base: i64 = lb as i64
126 sys_write(lfd, (base + j) as *u8, le2 - j)
127 _fp(lfd, "\n" as *u8)
128 }
129 }
130 j = le2 + 1
131 }
132 return cnt
133}
134// append the Doctor's work order to the queue (9 TSV columns, the queue's own format)
135func et_file_row(qp: *u8, ids: *u8, arc: *u8) -> i64 {
136 let fd: i64 = sys_openat_append(qp, 0x1a4)
137 if fd < 0 { return 0 }
138 _fp(fd, ids)
139 et_tab(fd); _fp(fd, "X" as *u8)
140 et_tab(fd); _fp(fd, "8" as *u8)
141 et_tab(fd); _fp(fd, "M" as *u8)
142 et_tab(fd); _fp(fd, "Doctor" as *u8)
143 et_tab(fd); _fp(fd, "TODO" as *u8)
144 et_tab(fd); _fp(fd, "-" as *u8)
145 et_tab(fd); _fp(fd, arc)
146 _fp(fd, "-gate-GREEN" as *u8)
147 et_tab(fd)
148 _fp(fd, "Doctor visit: " as *u8)
149 _fp(fd, arc)
150 _fp(fd, " arc gate RED/ABSENT -- failing rows named in knowledge/status/engineer_triage.log (Engineer filed; Doctor root-causes; Builder ships the ten-second probe; pulse re-proves)\n" as *u8)
151 sys_close(fd)
152 return 1
153}
154func et_log_arc(lfd: i64, arc: *u8, grade: i64, nf: i64, f: i64) -> i64 {
155 _fp(lfd, "ENGINEER-TRIAGE arc=" as *u8); _fp(lfd, arc)
156 _fp(lfd, " grade=" as *u8)
157 if grade == 1 { _fp(lfd, "RED" as *u8) }
158 if grade == 2 { _fp(lfd, "ABSENT" as *u8) }
159 _fp(lfd, " fail_lines=" as *u8); _fn(lfd, nf)
160 _fp(lfd, " filed=" as *u8); _fn(lfd, f)
161 _fp(lfd, "\n" as *u8)
162 _p(" " as *u8); _p(arc); _p(": defect triaged (see engineer_triage.log)\n" as *u8)
163 return 0
164}
165func main(argc: i64, argv: *i64) -> i64 {
166 var confp: *u8 = "knowledge/status/examiner_arcs.conf" as *u8
167 var qp: *u8 = "knowledge/registry/assignment_queue.tsv" as *u8
168 var logp: *u8 = "knowledge/status/engineer_triage.log" as *u8
169 if argc >= 2 { confp = argv[1] as *u8 }
170 if argc >= 3 { qp = argv[2] as *u8 }
171 if argc >= 4 { logp = argv[3] as *u8 }
172 _p("=== ENGINEER TRIAGE: red arc -> failing rows + a filed Doctor row, one command ===\n" as *u8)
173 let conf: *u8 = sys_mmap(K_MAGIC_65536)
174 let cn: i64 = et_read_all(confp, conf, K_MAGIC_65520)
175 if cn <= 0 { _p(" arcs conf MISSING -- fail loud\n" as *u8); sys_exit(1); return 1 }
176 let qbuf: *u8 = sys_mmap(K_MAGIC_1048576)
177 let qn: i64 = et_read_all(qp, qbuf, K_MAGIC_1048560)
178 if qn <= 0 { _p(" queue MISSING -- fail loud\n" as *u8); sys_exit(1); return 1 }
179 let lfd: i64 = sys_openat_append(logp, 0x1a4)
180 if lfd < 0 { _p(" triage log open failed\n" as *u8); sys_exit(1); return 1 }
181 let pool: *u8 = sys_mmap(K_MAGIC_16384)
182 let poffb: *i64 = sys_mmap(16) as *i64
183 poffb[0] = 0
184 let lb: *u8 = sys_mmap(K_MAGIC_1048592)
185 let span: *i64 = sys_mmap(16) as *i64
186 let ids: *u8 = sys_mmap(256)
187 var narcs: i64 = 0
188 var nbad: i64 = 0
189 var nfiled: i64 = 0
190 var ci: i64 = 0
191 while ci < cn {
192 var le: i64 = ci
193 var scan: i64 = 1
194 while scan == 1 {
195 if le >= cn { scan = 0 }
196 else { if conf[le] == (10 as u8) { scan = 0 } else { le = le + 1 } }
197 }
198 if conf[ci] == (65 as u8) {
199 let arc: i64 = (pool as i64) + poffb[0]
200 var t: i64 = et_token(conf, ci + 2, le, pool, poffb)
201 let glog: i64 = (pool as i64) + poffb[0]
202 t = et_token(conf, t + 1, le, pool, poffb)
203 let anchor: i64 = (pool as i64) + poffb[0]
204 t = et_token(conf, t + 1, le, pool, poffb)
205 let green: i64 = (pool as i64) + poffb[0]
206 t = et_token(conf, t + 1, le, pool, poffb)
207 narcs = narcs + 1
208 let ln: i64 = et_read_all(glog as *u8, lb, K_MAGIC_1048576)
209 var grade: i64 = 2
210 if ln > 0 {
211 if et_last_line_with(lb, ln, anchor as *u8, span) == 1 {
212 if et_slice_contains(lb, span[0], span[1], green as *u8) == 1 { grade = 0 } else { grade = 1 }
213 }
214 }
215 if grade != 0 {
216 nbad = nbad + 1
217 let nf: i64 = et_drill(lb, ln, anchor as *u8, lfd)
218 var o: i64 = et_cat(ids, 0, "ENG-" as *u8)
219 o = et_cat(ids, o, arc as *u8)
220 ids[o] = 0 as u8
221 var f: i64 = 0
222 if et_id_in_queue(qbuf, qn, ids) == 0 { f = et_file_row(qp, ids, arc as *u8); nfiled = nfiled + f }
223 et_log_arc(lfd, arc as *u8, grade, nf, f)
224 }
225 }
226 ci = le + 1
227 }
228 _fp(lfd, "ENGINEER-TRIAGE epoch=" as *u8); _fn(lfd, sys_now_realtime_sec())
229 _fp(lfd, " arcs=" as *u8); _fn(lfd, narcs)
230 _fp(lfd, " red_or_absent=" as *u8); _fn(lfd, nbad)
231 _fp(lfd, " filed=" as *u8); _fn(lfd, nfiled)
232 if nbad == 0 { _fp(lfd, " verdict=CLEAN\n" as *u8) } else { _fp(lfd, " verdict=TRIAGED\n" as *u8) }
233 sys_close(lfd)
234 _p(" --- TRIAGE: " as *u8); _p("arcs graded, defects filed -- durable: knowledge/status/engineer_triage.log ---\n" as *u8)
235 if nbad == 0 { _p(" ENGINEER TRIAGE: CLEAN (every arc green)\n" as *u8) } else { _p(" ENGINEER TRIAGE: TRIAGED (red arcs have filed Doctor rows + named failing lines)\n" as *u8) }
236 sys_exit(0)
237 return 0
238}