code wiki / (root) / nx_analyze_gate.nx

nx_analyze_gate.nx source

↩ module page · 179 lines · 9924 B

1// nx_analyze_gate.nx -- proves the analyst FRONT DOOR: the JSON an agent/workflow actually consumes is 2// well-formed and CORRECT, over real sharded seg_store data. The numeric organs are gated elsewhere; what 3// this gate owns is the SURFACE -- that the composed pipeline loads at scale, emits parseable JSON, and 4// that the honesty flags carry the right values. D001 verdict via nx_gate_verdict. expect_exit: 0 5import "nx_gate_verdict.nx" 6import "nx_analyze.nx" 7 8const AG_N: i64 = 300 // > one seg_store version window; forces the sharded path to matter 9const AG_SHARD: i64 = 150 10const AG_CAP: i64 = 65536 11 12func ag_lcg(s: i64) -> i64 { return (s * 1103515245 + 12345) & 0x7fffffff } 13func ag_has(hay: *u8, needle: *u8) -> i64 { 14 var hn: i64 = 0 15 while hay[hn] != (0 as u8) { hn = hn + 1 } 16 var nl: i64 = 0 17 while needle[nl] != (0 as u8) { nl = nl + 1 } 18 var i: i64 = 0 19 while i + nl <= hn { 20 var j: i64 = 0 21 var eq: i64 = 1 22 while j < nl { if hay[i+j] != needle[j] { eq = 0 } j = j + 1 } 23 if eq == 1 { return 1 } 24 i = i + 1 25 } 26 return 0 27} 28func ag_catn(buf: *u8, o: i64, v: i64) -> i64 { 29 var oo: i64 = o 30 var m: i64 = v 31 if m < 0 { buf[oo] = 45 as u8; oo = oo + 1; m = 0 - m } 32 let t: *u8 = sys_mmap(24) 33 var k: i64 = 0 34 if m == 0 { t[0] = 48 as u8; k = 1 } 35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 36 var i: i64 = k - 1 37 while i >= 0 { buf[oo] = t[i]; oo = oo + 1; i = i - 1 } 38 return oo 39} 40func ag_row(row: *u8, id: i64, seed: i64) -> i64 { 41 var s: i64 = ag_lcg(seed) 42 let size: i64 = 50 + (s % 150) 43 s = ag_lcg(s) 44 let age: i64 = s % 40 45 s = ag_lcg(s) 46 let price: i64 = 5 * size - 2 * age + (s % 20) 47 var ro: i64 = 0 48 ro = ag_catn(row, ro, size) 49 row[ro] = 9 as u8; ro = ro + 1 50 ro = ag_catn(row, ro, age) 51 row[ro] = 9 as u8; ro = ro + 1 52 ro = ag_catn(row, ro, price) 53 return ro 54} 55 56func main() -> i64 { 57 let ctr: *i64 = gv_ctr() 58 gv_head("nx_analyze_gate -- the agentic JSON front door, over sharded stored data" as *u8) 59 let out: *u8 = sys_mmap(AG_CAP) 60 61 // ---- pure verbs: the cheap primitives a workflow fans out on --------------------------------- 62 az_sig(994, 300, out) 63 gv_check("T1 sig JSON is well-formed and marks a real correlation significant" as *u8, ag_has(out, "\"verb\":\"sig\"" as *u8), ctr) 64 gv_check("T1b sig=994 n=300 -> significant true" as *u8, ag_has(out, "\"significant\":true" as *u8), ctr) 65 az_sig(50, 30, out) 66 gv_check("T2 sig=50 n=30 -> significant false (a machine field, not prose)" as *u8, ag_has(out, "\"significant\":false" as *u8), ctr) 67 az_ci(500, 30, 50, out) 68 gv_check("T3 ci JSON carries the hand-derived [170,728] bounds" as *u8, ag_has(out, "\"ci_lo_permille\":170" as *u8), ctr) 69 gv_check("T3b and the upper bound" as *u8, ag_has(out, "\"ci_hi_permille\":728" as *u8), ctr) 70 az_ci(500, 3, 50, out) 71 gv_check("T4 ci n=3 -> ok:false, no fabricated range" as *u8, ag_has(out, "\"ok\":false" as *u8), ctr) 72 73 // ---- report over REAL sharded data: price = 5*size - 2*age + noise, 300 rows ----------------- 74 let now: i64 = sys_now_us() 75 let prefix: *u8 = sys_mmap(128) 76 var po: i64 = 0 77 let pfx: *u8 = "/tmp/azgate_" as *u8 78 while pfx[po] != (0 as u8) { prefix[po] = pfx[po]; po = po + 1 } 79 po = ag_catn(prefix, po, now) 80 prefix[po] = 0 as u8 81 82 let w: *i64 = ss_begin() 83 let row: *u8 = sys_mmap(96) 84 let skey: *u8 = sys_mmap(64) 85 var i: i64 = 0 86 while i < AG_N { 87 asr_shard_key("row" as *u8, i / AG_SHARD, skey) 88 let rl: i64 = ag_row(row, i, 11 + i * 7) 89 ss_add(w, 1, skey, row, rl) 90 i = i + 1 91 } 92 ss_commit(prefix, w, now) 93 94 let names: *i64 = sys_mmap(8 * 3) as *i64 95 names[0] = "size" as *u8 as i64 96 names[1] = "age" as *u8 as i64 97 names[2] = "price" as *u8 as i64 98 // target = price (idx 2), nmax 400, up to 8 shards 99 az_report(prefix, "row" as *u8, 2, 400, 8, names, 3, out) 100 gv_puts(out) 101 gv_puts("\n---\n" as *u8) 102 103 gv_check("T5 report loaded ALL 300 rows across shards (scale path, not a 256 slice)" as *u8, ag_has(out, "\"rows\":300" as *u8), ctr) 104 gv_check("T6 and DECLARES it was not truncated (honest envelope)" as *u8, ag_has(out, "\"truncated\":false" as *u8), ctr) 105 gv_check("T7 target is named in the JSON" as *u8, ag_has(out, "\"target\":\"price\"" as *u8), ctr) 106 gv_check("T8 size relationship is present and family-significant (machine field)" as *u8, ag_has(out, "\"name\":\"size\",\"r_permille\":99" as *u8), ctr) 107 gv_check("T9 every relationship carries a confidence interval" as *u8, ag_has(out, "\"ci_lo\":" as *u8), ctr) 108 gv_check("T10 strongest association is size, with a partial-correlation control result" as *u8, ag_has(out, "\"strongest\":{\"name\":\"size\"" as *u8), ctr) 109 gv_check("T11 the strongest carries survives_control as a boolean an agent can branch on" as *u8, ag_has(out, "\"survives_control\":" as *u8), ctr) 110 gv_check("T12 the causal ceiling is stated as association-only (no unlicensed causal claim)" as *u8, ag_has(out, "\"causal\":\"association-only\"" as *u8), ctr) 111 112 // ---- ADVERSARY: a store with FAR more than one window under a SINGLE key must flag truncation. 113 // This proves the front door inherits F1001's honesty rather than silently analysing a slice. 114 let p2: *u8 = sys_mmap(128) 115 po = 0 116 let pfx2: *u8 = "/tmp/azgate2_" as *u8 117 while pfx2[po] != (0 as u8) { p2[po] = pfx2[po]; po = po + 1 } 118 po = ag_catn(p2, po, now) 119 p2[po] = 0 as u8 120 let w2: *i64 = ss_begin() 121 i = 0 122 while i < AG_N { 123 // everything under ONE key "row:0" -> the 300 rows exceed the 256 window 124 let rl: i64 = ag_row(row, i, 11 + i * 7) 125 ss_add(w2, 1, "row:0" as *u8, row, rl) 126 i = i + 1 127 } 128 ss_commit(p2, w2, now) 129 az_report(p2, "row" as *u8, 2, 400, 8, names, 3, out) 130 gv_check("T13 ADVERSARY 300 rows under one key -> the JSON reports truncated:true, never a silent slice" as *u8, ag_has(out, "\"truncated\":true" as *u8), ctr) 131 132 // ---- F1023: the `mine` verb -- no-target insight scan as JSON over the SAME sharded store -------- 133 // price=5*size-2*age+noise: size<->price and age<->price are the real relationships. This must come 134 // back as machine-readable findings, disclose the hypothesis count, and stay honest about multiplicity. 135 az_mine(prefix, "row" as *u8, 400, 8, names, 3, out) 136 gv_puts(out) 137 gv_puts(" 138--- 139" as *u8) 140 gv_check("T14 mine JSON is well-formed and labels the verb" as *u8, ag_has(out, "\"verb\":\"mine\"" as *u8), ctr) 141 gv_check("T15 it DISCLOSES the hypothesis count (3 pairs over 3 columns)" as *u8, ag_has(out, "\"hypotheses_tested\":3" as *u8), ctr) 142 gv_check("T16 findings are structured objects with a kind, not prose" as *u8, ag_has(out, "\"kind\":\"relationship\"" as *u8), ctr) 143 gv_check("T17 pairwise_certifiable is a machine boolean an agent reads (level exists at 3 pairs)" as *u8, ag_has(out, "\"pairwise_certifiable\":true" as *u8), ctr) 144 // the strongly-related size<->price pair must survive family-wise .05 and appear as a relationship 145 gv_check("T18 the real size<->price relationship is a structured finding, not prose" as *u8, ag_has(out, "\"a\":\"size\"" as *u8), ctr) 146 // and the SAME scan behind the text miner: the insight gate (19/19) proves noise-refusal at the organ 147 // level, so this gate owns only the VERB SURFACE. It must not double as a second insight gate. 148 149 // ---- F1006: the `ask` NL router -- questions in words route to the right verb ----------------- 150 az_ask("is a correlation of 994 over 300 significant" as *u8, 0 as *i64, 0, out) 151 gv_check("T19 a significance question routes to intent=sig" as *u8, ag_has(out, "\"intent\":\"sig\"" as *u8), ctr) 152 gv_check("T19b and actually answers it (significant:true embedded)" as *u8, ag_has(out, "\"significant\":true" as *u8), ctr) 153 az_ask("what is the confidence interval for 500 at 30" as *u8, 0 as *i64, 0, out) 154 gv_check("T20 a confidence-interval question routes to intent=ci (not sig, despite no 'significant')" as *u8, ag_has(out, "\"intent\":\"ci\"" as *u8), ctr) 155 gv_check("T20b and carries the hand-derived [170,728] bounds" as *u8, ag_has(out, "\"ci_lo_permille\":170" as *u8), ctr) 156 // report/mine over the sharded demo store built earlier (prefix/names in scope) 157 let ds: *i64 = sys_mmap(8 * 7) as *i64 158 ds[0] = prefix as i64 159 ds[1] = "row" as *u8 as i64 160 ds[2] = "400" as *u8 as i64 161 ds[3] = "8" as *u8 as i64 162 ds[4] = names[0] 163 ds[5] = names[1] 164 ds[6] = names[2] 165 az_ask("what has the strongest association with price" as *u8, ds, 7, out) 166 gv_check("T21 an association question routes to intent=report" as *u8, ag_has(out, "\"intent\":\"report\"" as *u8), ctr) 167 gv_check("T21b and RESOLVED the target from the word 'price' in the question" as *u8, ag_has(out, "\"target\":\"price\"" as *u8), ctr) 168 gv_check("T21c and produced the real analysis (size strongest)" as *u8, ag_has(out, "\"strongest\":{\"name\":\"size\"" as *u8), ctr) 169 az_ask("what is interesting in this data" as *u8, ds, 7, out) 170 gv_check("T22 an open-ended question routes to intent=mine" as *u8, ag_has(out, "\"intent\":\"mine\"" as *u8), ctr) 171 gv_check("T22b and discloses the hypothesis count" as *u8, ag_has(out, "\"hypotheses_tested\":3" as *u8), ctr) 172 // ADVERSARY: a sig/ci question with no numbers must FAIL cleanly, not fabricate 173 az_ask("is it significant" as *u8, 0 as *i64, 0, out) 174 gv_check("T23 ADVERSARY a significance question with NO numbers -> ok:false, nothing fabricated" as *u8, ag_has(out, "\"ok\":false" as *u8), ctr) 175 176 let rc: i64 = gv_verdict("ANALYZE-GATE" as *u8, ctr, "the analyst is callable as one MCP tool that answers in parseable JSON over sharded stored data" as *u8) 177 sys_exit(rc) 178 return rc 179}