code wiki / _hdl_build / nx_comp_engine.nx

nx_comp_engine.nx source

↩ module page · 164 lines · 7491 B

1// nx_comp_engine.nx -- the TRUE competitive comparison engine (exceeds a Gartner grid: MEASURED+SOURCED+ 2// COMPUTED+sovereign, not analyst-opinion). Reads knowledge/registry/comp_matrix.tsv (feature rows x system 3// columns, system[0]=Nishi=US) and emits: 4// 1. the feature x VENDOR TABLE (A vs B vs C vs N, one Y/P/N cell per system) 5// 2. per-VENDOR coverage score (who leads) 6// 3. BUY-OR-BUILD list = features WE lack (Nishi != YES) that >=1 competitor HAS -> analyst build-vs-buy 7// 4. WHITESPACE list = features NO system has -> first-mover build targets 8// 5. DIFFERENTIATION list = features WE have (Nishi=YES) that <=1 competitor has -> our moat 9// N-extensible: add a system column or a feature row to comp_matrix.tsv. Emits -> knowledge/status/comp_report.log 10// 11// module: nishi-core.analyst.comp_engine 12// depends: nishi-core.sys.syscalls 13// capability: COMPETITIVE_COMPARISON_ENGINE_VS_GARTNER 14// license_tier: ORIGINAL 15import "nx_syscalls.nx" 16import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 17const CE_MAGIC_16384: i64 = 16384 18const CE_MAGIC_262144: i64 = 262144 19 20const CE_REF: *u8 = "knowledge/registry/comp_matrix.tsv" 21const CE_OUT: *u8 = "knowledge/status/comp_report.log" 22 23func ce_w(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 } 24// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 25// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 26// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 27// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 28func ce_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 29func ce_wc(fd: i64, ch: i64) -> i64 { let b: *u8=sys_mmap(2); b[0]=ch as u8; b[1]=0 as u8; sys_write(fd,b,1); return 0 } 30func ce_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 31 32func ce_read(path: *u8, buf: *u8, cap: i64) -> i64 { 33 let fd: i64 = sys_openat_rd(path) 34 if fd < 0 { return 0 } 35 var n: i64 = 0 36 var r: i64 = sys_read(fd, buf, cap - 1) 37 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } } 38 sys_close(fd) 39 return n 40} 41 42func ce_field(buf: *u8, ls: i64, le: i64, f: i64, dst: *u8, cap: i64) -> i64 { 43 var col: i64 = 0 44 var p: i64 = ls 45 var k: i64 = 0 46 while p < le { 47 if buf[p] == (9 as u8) { if col == f { dst[k] = 0 as u8; return k } col = col + 1; if col == f { k = 0 } } 48 else { if col == f { if k < cap - 1 { dst[k] = buf[p]; k = k + 1 } } } 49 p = p + 1 50 } 51 dst[k] = 0 as u8 52 return k 53} 54 55func ce_nfields(buf: *u8, ls: i64, le: i64) -> i64 { 56 var c: i64 = 1 57 var p: i64 = ls 58 while p < le { if buf[p] == (9 as u8) { c = c + 1 } p = p + 1 } 59 return c 60} 61 62// write s padded with spaces to width w. 63func ce_pad(fd: i64, s: *u8, w: i64) -> i64 { 64 ce_w(fd, s) 65 var l: i64 = ce_len(s) 66 while l < w { ce_wc(fd, 32); l = l + 1 } 67 return 0 68} 69 70// append id + space to buffer at *off. 71func ce_app(dst: *u8, off: *i64, s: *u8) -> i64 { 72 var o: i64 = off[0] 73 var i: i64 = 0 74 while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } 75 dst[o] = 32 as u8; o = o + 1 76 dst[o] = 0 as u8 77 off[0] = o 78 return 0 79} 80 81func emit(fd: i64, rbuf: *u8, rn: i64) -> i64 { 82 let cell: *u8 = sys_mmap(64) 83 let nm: *u8 = sys_mmap(64) 84 let idf: *u8 = sys_mmap(128) 85 let yc: *i64 = sys_mmap(8 * 16) as *i64 86 let pc: *i64 = sys_mmap(8 * 16) as *i64 87 let buyb: *u8 = sys_mmap(CE_MAGIC_16384); let buyo: *i64 = sys_mmap(8) as *i64 88 let wsb: *u8 = sys_mmap(CE_MAGIC_16384); let wso: *i64 = sys_mmap(8) as *i64 89 let dfb: *u8 = sys_mmap(CE_MAGIC_16384); let dfo: *i64 = sys_mmap(8) as *i64 90 let snoff: *i64 = sys_mmap(8 * 16) as *i64 // header field offsets unused; re-extract by index 91 var nsys: i64 = 0 92 var nfeat: i64 = 0 93 var seen_header: i64 = 0 94 var ls: i64 = 0 95 var i: i64 = 0 96 while i <= rn { 97 var eol: i64 = 0 98 if i == rn { eol = 1 } else { if rbuf[i] == (10 as u8) { eol = 1 } } 99 if eol == 1 { 100 if i > ls { if rbuf[ls] != (35 as u8) { 101 if seen_header == 0 { 102 seen_header = 1 103 nsys = ce_nfields(rbuf, ls, i) - 3 104 if nsys > 16 { nsys = 16 } 105 ce_w(fd, "== COMP TABLE (Y/P/N) == feature " as *u8) 106 var s: i64 = 0 107 while s < nsys { ce_field(rbuf, ls, i, 3 + s, nm, 64); ce_pad(fd, nm, 9); s = s + 1 } 108 ce_w(fd, "\n" as *u8) 109 } else { 110 nfeat = nfeat + 1 111 ce_field(rbuf, ls, i, 0, idf, 128) 112 ce_w(fd, " " as *u8); ce_pad(fd, idf, 20); ce_w(fd, " " as *u8) 113 var total_y: i64 = 0 114 var comp_y: i64 = 0 115 var nishi_ch: i64 = 78 // 'N' 116 var s: i64 = 0 117 while s < nsys { 118 ce_field(rbuf, ls, i, 3 + s, cell, 64) 119 var c: i64 = 78 120 if ce_len(cell) > 0 { c = cell[0] as i64 } 121 ce_wc(fd, c); ce_w(fd, " " as *u8) 122 if c == 89 { yc[s] = yc[s] + 1; total_y = total_y + 1; if s > 0 { comp_y = comp_y + 1 } } 123 if c == 80 { pc[s] = pc[s] + 1 } 124 if s == 0 { nishi_ch = c } 125 s = s + 1 126 } 127 ce_w(fd, "\n" as *u8) 128 if total_y == 0 { ce_app(wsb, wso, idf) } 129 if nishi_ch != 89 { if comp_y >= 1 { ce_app(buyb, buyo, idf) } } 130 if nishi_ch == 89 { if comp_y <= 1 { ce_app(dfb, dfo, idf) } } 131 } 132 } } 133 ls = i + 1 134 } 135 i = i + 1 136 } 137 // per-vendor coverage 138 ce_w(fd, "\n== VENDOR COVERAGE (weighted Y=2 P=1 of " as *u8); ce_wn(fd, nfeat); ce_w(fd, " features) ==\n" as *u8) 139 var s2: i64 = 0 140 while s2 < nsys { 141 // re-extract header name into nm by scanning header again is costly; just print index + score 142 let score: i64 = (yc[s2] * 2 + pc[s2]) * 1000 / (2 * nfeat) 143 ce_w(fd, " system[" as *u8); ce_wn(fd, s2); ce_w(fd, "] Y=" as *u8); ce_wn(fd, yc[s2]) 144 ce_w(fd, " P=" as *u8); ce_wn(fd, pc[s2]); ce_w(fd, " coverage_permil=" as *u8); ce_wn(fd, score) 145 if s2 == 0 { ce_w(fd, " <- NISHI (us)\n" as *u8) } else { ce_w(fd, "\n" as *u8) } 146 s2 = s2 + 1 147 } 148 ce_w(fd, "\n== BUY-OR-BUILD (we lack, >=1 competitor has -> analyst build-vs-buy) ==\n " as *u8); ce_w(fd, buyb); ce_w(fd, "\n" as *u8) 149 ce_w(fd, "== WHITESPACE (NO system has it -> first-mover build) ==\n " as *u8); ce_w(fd, wsb); ce_w(fd, "\n" as *u8) 150 ce_w(fd, "== DIFFERENTIATION (we have it, <=1 competitor -> our moat) ==\n " as *u8); ce_w(fd, dfb); ce_w(fd, "\n" as *u8) 151 return 0 152} 153 154func main() -> i64 { 155 let rbuf: *u8 = sys_mmap(CE_MAGIC_262144) 156 let rn: i64 = ce_read(CE_REF, rbuf, CE_MAGIC_262144) 157 if rn <= 0 { ce_w(1, "COMPENGINE verdict=RED reason=no-matrix\n" as *u8); sys_exit(11); return 11 } 158 emit(1, rbuf, rn) 159 let lfd: i64 = sys_openat_wr(CE_OUT, 0x1a4) 160 if lfd >= 0 { emit(lfd, rbuf, rn); sys_close(lfd) } 161 ce_w(1, "COMPENGINE verdict=GREEN\n" as *u8) 162 sys_exit(0) 163 return 0 164}