code wiki / _hdl_build / nx_food_recipe_gate.nx

nx_food_recipe_gate.nx source

↩ module page · 207 lines · 11808 B

1// nx_food_recipe_gate.nx -- GATE: R-RECIPE. Mines the license-clean fetched corpora for ingredient 2// co-occurrence, validates the signal recovers the classic seeded pairings (research, not bro science), 3// and promotes the learned pairings into the durable store. Proves: T1 corpora mined, T2 LICENSE-GATED 4// mining (an inadmissible-tagged corpus is REFUSED -- negative control), T3 real signal found, T4 measured 5// validation (>=3 of the 15 seeded classic pairs independently confirmed by real recipe text; concrete 6// allium pairs printed), T5 promotion durable + readable, T6 facts-only (learned value is a bare count, no 7// prose). license_tier: ORIGINAL 8import "nx_food_recipe_mine.nx" 9import "nx_food_science.nx" 10import "nx_food_provenance.nx" 11import "nx_seg_store.nx" 12import "nx_syscalls.nx" 13import "nx_gate_verdict.nx" 14 15const RG_DUR: *u8 = "knowledge/store/food-" 16 17func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 18func g_i(v: i64) -> i64 { 19 let bb: *u8 = sys_mmap(28); var m: i64 = v 20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 21 let t: *u8 = sys_mmap(28); var k: i64 = 0 22 if m == 0 { t[0] = 48 as u8; k = 1 } 23 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 24 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 25} 26func g_name(world: *i64, idx: i64) -> i64 { let names: *i64 = world[4] as *i64; g_p(names[idx] as *u8); return 0 } 27 28func main() -> i64 { 29 g_p("=== nx_food_recipe_gate (R-RECIPE: ground pairings in real recipes) ===\n" as *u8) 30 31 // make sure the durable branch exists (idempotent) 32 fd_seed(RG_DUR) 33 fd_seed_provenance(RG_DUR) 34 let world: *i64 = fd_world_open(RG_DUR) 35 let n: i64 = world[0] 36 if n == 0 { g_p("FATAL empty world\nverdict=RED\n" as *u8); return 1 } 37 38 let termlc: *i64 = sys_mmap(8 * 64) as *i64 39 let termlen: *i64 = sys_mmap(8 * 64) as *i64 40 fm_build_terms(world, termlc, termlen) 41 let cooc: *i64 = sys_mmap(8 * n * n) as *i64 42 let pres: *i64 = sys_mmap(8 * 64) as *i64 43 44 // corpus list (path, license). Entry 5 is a NEGATIVE CONTROL: a real file tagged copyrighted (ARR) -> 45 // the license gate must REFUSE to mine it. 46 let paths: *i64 = sys_mmap(8 * 8) as *i64 47 let lics: *i64 = sys_mmap(8 * 8) as *i64 48 paths[0] = "knowledge/fetched/food_r2_wikibooks_cookbook.raw" as *u8 as i64; lics[0] = "CCBYSA" as *u8 as i64 49 paths[1] = "knowledge/fetched/food_r3_pd_cookbook.raw" as *u8 as i64; lics[1] = "PD" as *u8 as i64 50 paths[2] = "knowledge/fetched/food_c1_chinese.raw" as *u8 as i64; lics[2] = "CCBYSA" as *u8 as i64 51 paths[3] = "knowledge/fetched/food_t1_stir_frying.raw" as *u8 as i64; lics[3] = "CCBYSA" as *u8 as i64 52 paths[4] = "knowledge/fetched/food_r1_flavor_network.raw" as *u8 as i64; lics[4] = "CCBY" as *u8 as i64 53 paths[5] = "knowledge/fetched/food_c2_sichuan.raw" as *u8 as i64; lics[5] = "ARR" as *u8 as i64 54 let ncorp: i64 = 6 55 56 var mined: i64 = 0 57 var skipped: i64 = 0 58 var totalbytes: i64 = 0 59 var totalhits: i64 = 0 60 let szp: *i64 = sys_mmap(16) as *i64 61 var ci: i64 = 0 62 while ci < ncorp { 63 let path: *u8 = paths[ci] as *u8 64 let lic: *u8 = lics[ci] as *u8 65 if fd_source_admissible(RG_DUR, lic) == 0 { 66 g_p(" REFUSED (license " as *u8); g_p(lic); g_p("): " as *u8); g_p(path); g_p("\n" as *u8) 67 skipped = skipped + 1 68 } else { 69 let buf: *u8 = ss_readall(path, szp) 70 let sz: i64 = szp[0] 71 if sz > 0 { 72 fm_lower(buf, sz) 73 let hits: i64 = fm_mine(world, buf, sz, 800, termlc, termlen, cooc, pres) 74 g_p(" mined (" as *u8); g_p(lic); g_p(", " as *u8); g_i(sz); g_p("B, hits=" as *u8); g_i(hits); g_p("): " as *u8); g_p(path); g_p("\n" as *u8) 75 mined = mined + 1; totalbytes = totalbytes + sz; totalhits = totalhits + hits 76 } else { 77 g_p(" MISS (not on disk): " as *u8); g_p(path); g_p("\n" as *u8) 78 } 79 } 80 ci = ci + 1 81 } 82 83 var pass: i64 = 0 84 var tot: i64 = 0 85 86 // T1 corpora mined 87 tot = tot + 1 88 if mined >= 4 { if totalbytes > 100000 { pass = pass + 1; g_p("PASS T1 mined " as *u8); g_i(mined); g_p(" license-clean corpora (" as *u8); g_i(totalbytes); g_p(" bytes)\n" as *u8) } else { g_p("FAIL T1 too few bytes\n" as *u8) } } else { g_p("FAIL T1 too few corpora mined=" as *u8); g_i(mined); g_p("\n" as *u8) } 89 90 // T2 license-gated mining (negative control refused) 91 tot = tot + 1 92 if skipped == 1 { pass = pass + 1; g_p("PASS T2 license-gated: the ARR-tagged corpus was REFUSED (not mined)\n" as *u8) } else { g_p("FAIL T2 skipped=" as *u8); g_i(skipped); g_p("\n" as *u8) } 93 94 // T3 real signal 95 var npairs: i64 = 0 96 var i: i64 = 0 97 while i < n { var j: i64 = i + 1; while j < n { if cooc[i * n + j] > 0 { npairs = npairs + 1 } j = j + 1 } i = i + 1 } 98 tot = tot + 1 99 if npairs >= 10 { if totalhits > 0 { pass = pass + 1; g_p("PASS T3 signal: " as *u8); g_i(npairs); g_p(" distinct ingredient pairs co-occur in real recipe text\n" as *u8) } else { g_p("FAIL T3 no hits\n" as *u8) } } else { g_p("FAIL T3 too few pairs=" as *u8); g_i(npairs); g_p("\n" as *u8) } 100 101 // T4 measured validation: do the corpora confirm the classic seeded pairings? 102 let sa: *i64 = sys_mmap(8 * 16) as *i64 103 let sb: *i64 = sys_mmap(8 * 16) as *i64 104 sa[0] = "beef" as *u8 as i64; sb[0] = "oyster" as *u8 as i64 105 sa[1] = "beef" as *u8 as i64; sb[1] = "broccoli" as *u8 as i64 106 sa[2] = "beef" as *u8 as i64; sb[2] = "onion" as *u8 as i64 107 sa[3] = "chicken" as *u8 as i64; sb[3] = "garlic" as *u8 as i64 108 sa[4] = "pork" as *u8 as i64; sb[4] = "hoisin" as *u8 as i64 109 sa[5] = "shrimp" as *u8 as i64; sb[5] = "garlic" as *u8 as i64 110 sa[6] = "shrimp" as *u8 as i64; sb[6] = "snowpea" as *u8 as i64 111 sa[7] = "tofu" as *u8 as i64; sb[7] = "sesameoil" as *u8 as i64 112 sa[8] = "garlic" as *u8 as i64; sb[8] = "ginger" as *u8 as i64 113 sa[9] = "ginger" as *u8 as i64; sb[9] = "scallion" as *u8 as i64 114 sa[10] = "chili" as *u8 as i64; sb[10] = "garlic" as *u8 as i64 115 sa[11] = "mushroom" as *u8 as i64; sb[11] = "oyster" as *u8 as i64 116 sa[12] = "bellpepper" as *u8 as i64; sb[12] = "blackbean" as *u8 as i64 117 sa[13] = "broccoli" as *u8 as i64; sb[13] = "oyster" as *u8 as i64 118 sa[14] = "beansprout" as *u8 as i64; sb[14] = "scallion" as *u8 as i64 119 var confirmed: i64 = 0 120 var sp: i64 = 0 121 while sp < 15 { 122 let ia: i64 = fd_index_of(world, sa[sp] as *u8) 123 let ib: i64 = fd_index_of(world, sb[sp] as *u8) 124 if ia >= 0 { if ib >= 0 { if cooc[ia * n + ib] > 0 { confirmed = confirmed + 1 } } } 125 sp = sp + 1 126 } 127 // concrete allium evidence 128 let gi: i64 = fd_index_of(world, "garlic" as *u8) 129 let gn: i64 = fd_index_of(world, "ginger" as *u8) 130 let on: i64 = fd_index_of(world, "onion" as *u8) 131 let bf: i64 = fd_index_of(world, "beef" as *u8) 132 g_p(" seeded classic pairs confirmed by real recipes: " as *u8); g_i(confirmed); g_p("/15\n" as *u8) 133 g_p(" e.g. garlic+ginger=" as *u8); g_i(cooc[gi * n + gn]) 134 g_p(" onion+garlic=" as *u8); g_i(cooc[on * n + gi]) 135 g_p(" beef+onion=" as *u8); g_i(cooc[bf * n + on]); g_p("\n" as *u8) 136 // top learned pairs (raw signal) 137 let tmp: *i64 = sys_mmap(8 * n * n) as *i64 138 var ti: i64 = 0; while ti < n * n { tmp[ti] = cooc[ti]; ti = ti + 1 } 139 g_p(" top learned pairs:" as *u8) 140 var tk: i64 = 0 141 while tk < 6 { 142 var bi: i64 = 0 - 1; var bj: i64 = 0 - 1; var bv: i64 = 0 143 var x: i64 = 0 144 while x < n { var y: i64 = x + 1; while y < n { if tmp[x * n + y] > bv { bv = tmp[x * n + y]; bi = x; bj = y } y = y + 1 } x = x + 1 } 145 if bv > 0 { g_p(" " as *u8); g_name(world, bi); g_p("+" as *u8); g_name(world, bj); g_p("=" as *u8); g_i(bv); tmp[bi * n + bj] = 0; tmp[bj * n + bi] = 0 } else { tk = 6 } 146 tk = tk + 1 147 } 148 g_p("\n" as *u8) 149 tot = tot + 1 150 if confirmed >= 3 { pass = pass + 1; g_p("PASS T4 validation: real recipe corpora independently CONFIRM the classic pairings (>=3 of 15) -- research, not bro science\n" as *u8) } else { g_p("FAIL T4 only " as *u8); g_i(confirmed); g_p(" seed pairs confirmed\n" as *u8) } 151 152 // T5 promote durable + readable 153 let written: i64 = fm_promote(RG_DUR, world, cooc) 154 g_p(" promoted " as *u8); g_i(written); g_p(" learned-pair records into knowledge/store/food-\n" as *u8) 155 let pq: *i64 = sys_mmap(16) as *i64 156 let lq: *i64 = sys_mmap(16) as *i64 157 var readback: i64 = 0 158 let lkey: *u8 = sys_mmap(96) 159 var lo: i64 = 0 160 lo = as_append(lkey, lo, "food:pairlearned:garlic:ginger" as *u8); lkey[lo] = 0 as u8 161 if ss_get(RG_DUR, lkey, pq, lq) == 1 { readback = 1 } 162 // a learned record may already be durable from a prior pass (idempotent skip -> written=0); the 163 // guarantee is that the learned pairs are DURABLE + READABLE, not that THIS pass wrote them. 164 let lkey2: *u8 = sys_mmap(96) 165 var lo2: i64 = 0 166 lo2 = as_append(lkey2, lo2, "food:pairlearned:beef:ginger" as *u8); lkey2[lo2] = 0 as u8 167 var readback2: i64 = 0 168 if ss_get(RG_DUR, lkey2, pq, lq) == 1 { readback2 = 1 } 169 tot = tot + 1 170 var ok5: i64 = 1 171 if readback != 1 { ok5 = 0 } 172 if readback2 != 1 { ok5 = 0 } 173 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 promotion durable + readable (learned pairs present; this pass wrote " as *u8); g_i(written); g_p(" new, additive/idempotent)\n" as *u8) } else { g_p("FAIL T5 readback=" as *u8); g_i(readback); g_p(" readback2=" as *u8); g_i(readback2); g_p("\n" as *u8) } 174 175 // T6 facts-only: the learned value is a bare integer count (no prose) 176 tot = tot + 1 177 var ok6: i64 = 1 178 if readback == 1 { 179 let b: *u8 = pq[0] as *u8 180 let bl: i64 = lq[0] 181 if bl == 0 { ok6 = 0 } 182 var d: i64 = 0 183 while d < bl { let cc: i64 = b[d] as i64; if cc < 48 { ok6 = 0 } if cc > 57 { ok6 = 0 } d = d + 1 } 184 } else { ok6 = 0 } 185 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 facts-only: learned record is a bare ingredient-pair count, no prose stored\n" as *u8) } else { g_p("FAIL T6\n" as *u8) } 186 187 // T7 LOOP CLOSED: re-open the durable world (now carrying the learned pairings) and show the engine's 188 // pairing affinity for an evidence-backed-but-unseeded pair is lifted above baseline. 189 let w2: *i64 = fd_world_open(RG_DUR) 190 let xbf: i64 = fd_index_of(w2, "beef" as *u8) 191 let xgn: i64 = fd_index_of(w2, "ginger" as *u8) 192 let affbg: i64 = fd_pair_aff(w2, xbf, xgn) 193 g_p(" beef+ginger affinity now=" as *u8); g_i(affbg); g_p(" (unseeded baseline=" as *u8); g_i(FOOD_AFF_BASE); g_p(", lifted by real-recipe co-occurrence)\n" as *u8) 194 tot = tot + 1 195 if affbg > FOOD_AFF_BASE { pass = pass + 1; g_p("PASS T7 loop closed: real recipes now move the chooser (learned pairing > baseline)\n" as *u8) } else { g_p("FAIL T7 learned evidence not reflected (aff=" as *u8); g_i(affbg); g_p(")\n" as *u8) } 196 197 g_p("nx_food_recipe_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 198 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 199 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 200 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 201 let ctr__dry: *i64 = gv_ctr() 202 ctr__dry[0] = pass 203 ctr__dry[1] = tot 204 let rc__dry: i64 = gv_verdict("FOOD-RECIPE-GATE" as *u8, ctr__dry, "pairings GROUNDED in real license-clean recipes; learned-vs-seed auditable)" as *u8) 205 sys_exit(rc__dry) 206 return rc__dry 207}