code wiki / _hdl_build / nx_analyst_gate.nx

nx_analyst_gate.nx source

↩ module page · 112 lines · 6579 B

1// nx_analyst_gate.nx -- ENGINEER gate for the ANALYST organ (RACI: Builder authored nx_analyst.nx, 2// the ENGINEER verifies it here, the Doctor heals only on RED). KATs prove the four verdicts fire 3// for the right reasons, tampers prove the gate goes RED when common-wisdom sneaks in or the 4// arithmetic breaks, and the paradox lens is checked against a hand-computed fixed-point value. 5// Synthetic ledger shaped like the AC case (equipment/labor/permits/channel) -- REAL corroborated 6// numbers live in nx_analyst_case_ac.nx, not here. license_tier: ORIGINAL 7import "nx_analyst.nx" 8import "nx_syscalls.nx" 9 10// gate spec thresholds (the contract under test, mirrored by the case modules) 11const AG_TOL_PPM: i64 = 5000 // interior closure tolerance: 0.5% 12const AG_MIN_TP: i64 = 600 // <60.0% traced -> UNDERTRACED, no verdict allowed 13const AG_MAX_RP: i64 = 150 // >15.0% unexplained residual -> SHENANIGAN, named 14 15func ag_wf(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 } 16func ag_wnf(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 } 17func ag_w(s: *u8) -> i64 { return ag_wf(1, s) } 18func ag_wn(v: i64) -> i64 { return ag_wnf(1, v) } 19 20// build the 9-node synthetic AC ledger into (parent,value,kind). 21// 0 ROOT install price 700000c | 1 equipment(DERIVED) 250000 | 2 labor 200000 | 3 permits 50000 22// 4 channel-markup 200000 | 5..8 equipment children: compressor 90000, coil 60000, 23// electronics 40000, housing 60000. All leaves MEASURED. Closes exactly, residual 0. 24func ag_build(parent: *i64, value: *i64, kind: *i64) -> i64 { 25 parent[0]=0-1; value[0]=700000; kind[0]=AN_DERIVED 26 parent[1]=0; value[1]=250000; kind[1]=AN_DERIVED 27 parent[2]=0; value[2]=200000; kind[2]=AN_MEASURED 28 parent[3]=0; value[3]=50000; kind[3]=AN_MEASURED 29 parent[4]=0; value[4]=200000; kind[4]=AN_MEASURED 30 parent[5]=1; value[5]=90000; kind[5]=AN_MEASURED 31 parent[6]=1; value[6]=60000; kind[6]=AN_MEASURED 32 parent[7]=1; value[7]=40000; kind[7]=AN_MEASURED 33 parent[8]=1; value[8]=60000; kind[8]=AN_MEASURED 34 return 9 35} 36 37func main() -> i64 { 38 let parent: *i64 = sys_mmap(8*16) as *i64 39 let value: *i64 = sys_mmap(8*16) as *i64 40 let kind: *i64 = sys_mmap(8*16) as *i64 41 let r: *i64 = sys_mmap(8*16) as *i64 42 var t: i64 = 0 43 44 // KAT1: clean closing ledger -> EXPLAINED (coverage 1000, residual 0) 45 let n: i64 = ag_build(parent, value, kind) 46 r[t]=0; if an_ledger_verdict(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == AN_EXPLAINED { r[t]=1 } t=t+1 47 48 // KAT2: structural checks on the clean ledger 49 r[t]=0; if an_closure_fails(parent,value,n,AG_TOL_PPM) == 0 { r[t]=1 } t=t+1 50 r[t]=0; if an_traced_cents(parent,value,kind,n) == 700000 { r[t]=1 } t=t+1 51 r[t]=0; if an_traced_permil(700000, 700000) == 1000 { r[t]=1 } t=t+1 52 r[t]=0; if an_residual_permil(700000, 700000) == 0 { r[t]=1 } t=t+1 53 54 // KAT3: SHENANIGAN -- channel-markup row is only a DERIVED placeholder (couldn't source it): 55 // ledger still closes, coverage 714 permil >= 600, residual 286 permil > 150 -> NAMED residual. 56 kind[4]=AN_DERIVED 57 r[t]=0; if an_traced_cents(parent,value,kind,n) == 500000 { r[t]=1 } t=t+1 58 r[t]=0; if an_ledger_verdict(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == AN_SHENANIGAN { r[t]=1 } t=t+1 59 kind[4]=AN_MEASURED 60 61 // TAMPER1: common-wisdom sneaks in -- markup becomes a NARRATIVE claim with a cost -> BROKEN (RED) 62 kind[4]=AN_NARRATIVE 63 r[t]=0; if an_narrative_admitted(kind,value,n) == 1 { r[t]=1 } t=t+1 64 r[t]=0; if an_ledger_verdict(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == AN_BROKEN { r[t]=1 } t=t+1 65 r[t]=0; if an_fetch_tasks_owed(kind,n) == 1 { r[t]=1 } t=t+1 // the narrative owes the Researcher a task 66 kind[4]=AN_MEASURED 67 68 // TAMPER2: arithmetic break -- compressor +50000 so equipment no longer closes -> BROKEN (RED) 69 value[5]=140000 70 r[t]=0; if an_closure_fails(parent,value,n,AG_TOL_PPM) != 0 { r[t]=1 } t=t+1 71 r[t]=0; if an_ledger_verdict(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == AN_BROKEN { r[t]=1 } t=t+1 72 value[5]=90000 73 74 // KAT4: UNDERTRACED -- only 30% of the price has provenance -> honest "don't know yet" 75 // (markup+labor+permits unsourced placeholders: traced = 250000 of 700000 = 357 permil < 600) 76 kind[2]=AN_DERIVED; kind[3]=AN_DERIVED; kind[4]=AN_DERIVED 77 r[t]=0; if an_ledger_verdict(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == AN_UNDERTRACED { r[t]=1 } t=t+1 78 kind[2]=AN_MEASURED; kind[3]=AN_MEASURED; kind[4]=AN_MEASURED 79 80 // KAT5: double-count guard -- negative residual is BROKEN, never a verdict 81 r[t]=0; if an_verdict(0, 0, 1071, 0-71, AG_MIN_TP, AG_MAX_RP) == AN_BROKEN { r[t]=1 } t=t+1 82 83 // KAT6: the paradox lens, hand-computed -- AC $7000/1500 parts vs PC $800/2000 parts: 84 // per-complexity 466c vs 40c -> q10 = 466*1024/40 = 11929 (~11.6x pricier per unit complexity) 85 r[t]=0; if an_price_per_complexity(700000, 1500) == 466 { r[t]=1 } t=t+1 86 r[t]=0; if an_price_per_complexity(80000, 2000) == 40 { r[t]=1 } t=t+1 87 r[t]=0; if an_paradox_q10(700000, 1500, 80000, 2000) == 11929 { r[t]=1 } t=t+1 88 89 // KAT7: determinism -- same ledger, same verdict, twice 90 r[t]=0; if an_is_reproducible(parent,value,kind,n,AG_TOL_PPM,AG_MIN_TP,AG_MAX_RP) == 1 { r[t]=1 } t=t+1 91 92 var pass: i64 = 0 93 var i: i64 = 0 94 while i < t { pass = pass + r[i]; i = i + 1 } 95 ag_w("=== ANALYST GATE (Engineer): cost-ledger trace, verdicts, tampers, paradox lens ===\n" as *u8) 96 var j: i64 = 0 97 while j < t { 98 if r[j] == 0 { ag_w(" FAIL kat#" as *u8); ag_wn(j); ag_w("\n" as *u8) } 99 j = j + 1 100 } 101 ag_w(" passed " as *u8); ag_wn(pass); ag_w("/" as *u8); ag_wn(t); ag_w("\n" as *u8) 102 // durable anchor for the Examiner's arc table (grade = LAST ANALYST-GATE line) 103 let dfd: i64 = sys_openat_append("knowledge/status/analyst_gate.log" as *u8, 0x1a4) 104 if dfd >= 0 { 105 if pass == t { ag_wf(dfd, "ANALYST-GATE verdict=GREEN " as *u8) } 106 if pass != t { ag_wf(dfd, "ANALYST-GATE verdict=RED " as *u8) } 107 ag_wnf(dfd, pass); ag_wf(dfd, "/" as *u8); ag_wnf(dfd, t); ag_wf(dfd, "\n" as *u8) 108 sys_close(dfd) 109 } 110 if pass == t { sys_exit(0); return 0 } 111 sys_exit(1); return 1 112}