code wiki / (root) / nx_treatment.nx

nx_treatment.nx source

↩ module page · 141 lines · 6949 B

1// nx_treatment.nx -- CLI over nx_treatment_lib: does this board declare the three-step treatment (mcp, api, primitive, 2// workflow, agent) per rung, and does each declaration verify against the estate? 3// nx_treatment <domain> per treat row a line with its five field states, the rungs with no row, the census, verdict LAST 4// exit: 0 TREATED | 1 PARTIAL (a declared row unmet: the hard fail) | 2 usage | 3 NO-TREATMENT or NO-PLAN 5// Evidence: tool_allowlist.conf (the MCP and API surfaces), the source tree through ep_src_path (the primitive), the 6// plan_run plane manifest knowledge/store/plan-<id>-manifest.txt (the workflow), the clock DESIRED plane loaded 7// through nx_store_seed_lib and knowledge/gateroster.conf (the agent). Paths resolve from either cwd through 8// nx_estate_path, so the seat and the ranker read the same evidence. 9// license_tier: ORIGINAL No hw writes (Rule 26). 10import "nx_syscalls.nx" 11import "nx_estate_path.nx" 12import "nx_barfresh_lib.nx" 13import "nx_store_seed_lib.nx" 14import "nx_treatment_lib.nx" 15 16const TC_ARG_DOMAIN: i64 = 1 17const TC_EXIT_USAGE: i64 = 2 18const TC_EXIT_NOPLAN: i64 = 3 19const TC_STDOUT: i64 = 1 20const TC_NUM_CAP: i64 = 24 21const TC_PATH_CAP: i64 = 1024 22const TC_ALLOW: *u8 = "tool_allowlist.conf" 23const TC_ROSTER: *u8 = "knowledge/gateroster.conf" 24const TC_CLOCK_PLANE: *u8 = "knowledge/store/clockjobs-" 25const TC_PLANE_CAP: i64 = 1048576 // the clock DESIRED plane read: 188 rows on 2026-09-16; a fill is announced UNPROVEN 26const TC_WF_PFX: *u8 = "knowledge/store/plan-" 27const TC_WF_SFX: *u8 = "-manifest.txt" 28const TC_MISS_CAP: i64 = 256 29 30func tc_w(s: *u8) -> i64 { return sys_write(TC_STDOUT, s, trt_slen(s)) } 31func tc_wn(v: i64) -> i64 { 32 let b: *u8 = sys_mmap(TC_NUM_CAP) 33 var x: i64 = v 34 var neg: i64 = 0 35 if x < 0 { neg = 1; x = 0 - x } 36 var o: i64 = TC_NUM_CAP - 1 37 b[o] = 0 as u8 38 o = o - 1 39 if x == 0 { b[o] = 48 as u8; o = o - 1 } 40 while x > 0 { b[o] = (x - (x / 10) * 10 + 48) as u8; o = o - 1; x = x / 10 } 41 if neg == 1 { b[o] = 45 as u8; o = o - 1 } 42 return tc_w((b as i64 + o + 1) as *u8) 43} 44func tc_wspan(buf: *u8, off: i64, len: i64) -> i64 { 45 if len <= 0 { return 0 } 46 return sys_write(TC_STDOUT, (buf as i64 + off) as *u8, len) 47} 48func tc_span_copy(buf: *u8, off: i64, len: i64) -> *u8 { 49 var l: i64 = len 50 if l < 0 { l = 0 } 51 let s: *u8 = sys_mmap(l + 1) 52 var i: i64 = 0 53 while i < l { s[i] = buf[off + i]; i = i + 1 } 54 s[l] = 0 as u8 55 return s 56} 57func tc_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var oo: i64 = o; while s[i] != (0 as u8) { d[oo] = s[i]; oo = oo + 1; i = i + 1 } d[oo] = 0 as u8; return oo } 58 59// read a whole file resolved through the estate path probe; 0 bytes when absent 60func tc_read_estate(rel: *u8, outp: *i64) -> i64 { 61 let path: *u8 = sys_mmap(TC_PATH_CAP) 62 if ep_artifact_path(path, rel) == 0 { outp[0] = 0; return 0 } 63 let np: *i64 = sys_mmap(8) as *i64 64 np[0] = 0 65 let b: *u8 = sys_read_file(path, np) 66 outp[0] = b as i64 67 return np[0] 68} 69 70func main(argc: i64, argv: *i64) -> i64 { 71 if argc < 2 { 72 tc_w("usage: nx_treatment <domain> -- exit 0 TREATED | 1 PARTIAL | 3 NO-TREATMENT or NO-PLAN\n" as *u8) 73 return TC_EXIT_USAGE 74 } 75 let dom: *u8 = argv[TC_ARG_DOMAIN] as *u8 76 let lenp: *i64 = sys_mmap(8) as *i64 77 let which: *i64 = sys_mmap(8) as *i64 78 lenp[0] = 0 79 let plan: *u8 = bf_read_plan(ct_first_published(), ct_second_published(), dom, lenp, which) 80 let pn: i64 = lenp[0] 81 if pn <= 0 { tc_w("TREATMENT domain=" as *u8); tc_w(dom); tc_w(" plan=UNREADABLE-IN-BOTH-TREES verdict=NO-PLAN\n" as *u8); return TC_EXIT_NOPLAN } 82 // the evidence 83 let ap: *i64 = sys_mmap(8) as *i64 84 let an: i64 = tc_read_estate(TC_ALLOW, ap) 85 let rp: *i64 = sys_mmap(8) as *i64 86 let rn: i64 = tc_read_estate(TC_ROSTER, rp) 87 let clock: *u8 = sys_mmap(TC_PLANE_CAP) 88 var cn: i64 = sts_load(TC_CLOCK_PLANE, clock, TC_PLANE_CAP) 89 var clock_unproven: i64 = 0 90 if cn >= TC_PLANE_CAP - 1 { clock_unproven = 1 } 91 if cn < 0 { cn = 0 } 92 tc_w("TREATMENT-EVIDENCE allowlist_bytes=" as *u8); tc_wn(an); tc_w(" roster_bytes=" as *u8); tc_wn(rn); tc_w(" clock_plane_bytes=" as *u8); tc_wn(cn) 93 if clock_unproven == 1 { tc_w(" clock_plane=FILLED-ITS-RESERVE(agent reads are UNPROVEN)" as *u8) } 94 tc_w("\n" as *u8) 95 let cap: i64 = trt_cap(pn) 96 let tab: *i64 = sys_mmap(cap * TRT_STRIDE * 8) as *i64 97 let rows: i64 = trt_scan(plan, pn, tab, cap) 98 let sp: *u8 = sys_mmap(TC_PATH_CAP) 99 let wfp: *u8 = sys_mmap(TC_PATH_CAP) 100 let mb: *u8 = sys_mmap(TC_MISS_CAP) 101 var r: i64 = 0 102 while r < rows { 103 let b: i64 = r * TRT_STRIDE 104 var prim_ok: i64 = 0 - 1 105 var wf_ok: i64 = 0 - 1 106 if tab[b + TRT_ST_PRIM] == TRT_F_DECLARED { 107 let pname: *u8 = tc_span_copy(plan, tab[b + TRT_PRIM_O], tab[b + TRT_PRIM_L]) 108 prim_ok = ep_src_path(sp, pname) 109 } 110 if tab[b + TRT_ST_WF] == TRT_F_DECLARED { 111 var o: i64 = tc_cat(wfp, 0, TC_WF_PFX) 112 let wname: *u8 = tc_span_copy(plan, tab[b + TRT_WF_O], tab[b + TRT_WF_L]) 113 o = tc_cat(wfp, o, wname) 114 o = tc_cat(wfp, o, TC_WF_SFX) 115 let probe: *u8 = sys_mmap(TC_PATH_CAP) 116 wf_ok = ep_artifact_path(probe, wfp) 117 } 118 var cnj: i64 = cn 119 if clock_unproven == 1 { cnj = 0 } 120 let v: i64 = trt_judge_row(tab, r, plan, ap[0] as *u8, an, rp[0] as *u8, rn, clock, cnj, prim_ok, wf_ok) 121 tc_w(" TREAT rung=" as *u8); tc_wspan(plan, tab[b + TRT_RUNG_O], tab[b + TRT_RUNG_L]) 122 tc_w(" mcp=" as *u8); tc_w(trt_state_name(tab[b + TRT_ST_MCP])) 123 tc_w(" api=" as *u8); tc_w(trt_state_name(tab[b + TRT_ST_API])) 124 tc_w(" prim=" as *u8); tc_w(trt_state_name(tab[b + TRT_ST_PRIM])) 125 tc_w(" wf=" as *u8); tc_w(trt_state_name(tab[b + TRT_ST_WF])) 126 tc_w(" agent=" as *u8); tc_w(trt_state_name(tab[b + TRT_ST_AGENT])) 127 tc_w(" verdict=" as *u8); tc_w(trt_verdict_name(v)) 128 if v != TRT_V_COMPLETE { if v != TRT_V_FIELDS { trt_missing(tab, r, mb, 0); tc_w(" missing=" as *u8); tc_w(mb) } } 129 if v == TRT_V_FIELDS { tc_w(" (a treat row has seven pipe fields: treat|rung|mcp=|api=|prim=|wf=|agent=)" as *u8) } 130 tc_w("\n" as *u8) 131 r = r + 1 132 } 133 let c: *i64 = sys_mmap(TRT_C_SLOTS * 8) as *i64 134 trt_census(tab, rows, plan, pn, c) 135 tc_w("TREATMENT domain=" as *u8); tc_w(dom) 136 tc_w(" rows=" as *u8); tc_wn(c[TRT_C_ROWS]); tc_w(" complete=" as *u8); tc_wn(c[TRT_C_COMPLETE]); tc_w(" partial=" as *u8); tc_wn(c[TRT_C_PARTIAL]); tc_w(" fields=" as *u8); tc_wn(c[TRT_C_FIELDS]); tc_w(" sum=" as *u8); tc_wn(trt_partition_sum(c)) 137 tc_w(" rungs=" as *u8); tc_wn(c[TRT_C_RUNGS]); tc_w(" undeclared=" as *u8); tc_wn(c[TRT_C_UNDECLARED]) 138 let v2: i64 = trt_verdict(c) 139 tc_w(" verdict=" as *u8); tc_w(trt_exit_name(v2)); tc_w("\n" as *u8) 140 return v2 141}