code wiki / _hdl_build / nx_food_stirfry_gate.nx

nx_food_stirfry_gate.nx source

↩ module page · 197 lines · 13435 B

1// nx_food_stirfry_gate.nx -- GATE: proves the Nishi food-science stir-fry chooser (rung 0 of the food 2// branch). Seeds a hermetic temp store, opens the world, and asserts the capability across nine 3// dimensions: T1 deterministic, T2 role-complete (a real stir-fry: 1 protein/2 veg/1 aromatic/1 sauce), 4// T3 preference-responsive (more spicy preference -> a spicier build), T4 availability-constrained (an 5// unavailable ingredient never appears), T5 allergen hard-exclude (an avoided ingredient never appears), 6// T6 negative-control / liar-kill (no protein -> honest INCOMPLETE not a fabricated pick; unknown id is 7// rejected LOUD), T7 MEASURED EXCEED (the chosen build beats a naive strongest-ingredient build on the 8// published food-science objective -- capability is the exceed), T8 sovereignty floor (0 third-party JS), 9// T9 teach-the-builder (a food domain is registered into the universal catalog -- the team GROWS). 10// Emits a preview to knowledge/staging/food/page.html. license_tier: ORIGINAL 11import "nx_food_science.nx" 12import "nx_nishi_builder.nx" 13import "nx_ad_serve.nx" 14import "nx_syscalls.nx" 15 16const FG_PREFIX: *u8 = "/tmp/food_g-" 17const FG_CAT: *u8 = "/tmp/nishi_food_gate.tsv" 18 19func 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 } 20func g_i(v: i64) -> i64 { 21 let bb: *u8 = sys_mmap(28); var m: i64 = v 22 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 23 let t: *u8 = sys_mmap(28); var k: i64 = 0 24 if m == 0 { t[0] = 48 as u8; k = 1 } 25 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 26 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 27} 28func g_name(h: *i64, idx: i64) -> i64 { let names: *i64 = h[4] as *i64; g_p(names[idx] as *u8); return 0 } 29func g_show(h: *i64, build: *i64, nb: i64) -> i64 { 30 var i: i64 = 0 31 while i < nb { if i > 0 { g_p(" | " as *u8) } g_name(h, build[i]); i = i + 1 } 32 g_p("\n" as *u8); return 0 33} 34func g_count_role(roles: *i64, nb: i64, rc: i64) -> i64 { var c: i64 = 0; var i: i64 = 0; while i < nb { if roles[i] == rc { c = c + 1 } i = i + 1 } return c } 35func g_in_build(build: *i64, nb: i64, idx: i64) -> i64 { var i: i64 = 0; while i < nb { if build[i] == idx { return 1 } i = i + 1 } return 0 } 36func g_bytes_eq(a: *u8, la: i64, b: *u8, lb: i64) -> i64 { if la != lb { return 0 } var i: i64 = 0; while i < la { if a[i] != b[i] { return 0 } i = i + 1 } return 1 } 37 38func main() -> i64 { 39 g_p("=== nx_food_stirfry_gate ===\n" as *u8) 40 41 let wrote: i64 = fd_seed(FG_PREFIX) 42 g_p("seeded food-science store (records written this run=" as *u8); g_i(wrote); g_p(")\n" as *u8) 43 44 let h: *i64 = fd_world_open(FG_PREFIX) 45 let n: i64 = h[0] 46 g_p("world: ingredients=" as *u8); g_i(n); g_p(" pairings=" as *u8); g_i(h[5]); g_p("\n" as *u8) 47 if n == 0 { g_p("FATAL: empty world (store seed/open failed)\nverdict=RED\n" as *u8); return 1 } 48 49 let frame: *i64 = sys_mmap(8 * 16) as *i64 50 let nframe: i64 = fd_load_frame(FG_PREFIX, frame) 51 let wts: *i64 = sys_mmap(8 * 8) as *i64 52 fd_load_weights(FG_PREFIX, wts) 53 g_p("frame slots=" as *u8); g_i(nframe); g_p(" weights[Krole,Wbal,Waff,Wpref,MAXIMB]=" as *u8) 54 var wi: i64 = 0; while wi < 5 { g_p(" " as *u8); g_i(wts[wi]); wi = wi + 1 } g_p("\n" as *u8) 55 56 let avail: *i64 = sys_mmap(8 * 64) as *i64 57 let avoid: *i64 = sys_mmap(8 * 64) as *i64 58 let prefw: *i64 = sys_mmap(8 * 8) as *i64 59 60 var pass: i64 = 0 61 var tot: i64 = 0 62 63 // -------- T1 determinism -------- 64 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 65 var a0: i64 = 0; while a0 < FOOD_NAX { prefw[a0] = 0; a0 = a0 + 1 } 66 let b1: *i64 = sys_mmap(8 * 16) as *i64; let r1: *i64 = sys_mmap(8 * 16) as *i64 67 let b2: *i64 = sys_mmap(8 * 16) as *i64; let r2: *i64 = sys_mmap(8 * 16) as *i64 68 let nb1: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b1, r1) 69 let nb2: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b2, r2) 70 g_p("neutral pick: " as *u8); g_show(h, b1, nb1) 71 let pg1: *u8 = sys_mmap(32768); let pn1: i64 = fd_emit_page(h, b1, r1, nb1, prefw, wts, "Golden Wok" as *u8, pg1) 72 let pg2: *u8 = sys_mmap(32768); let pn2: i64 = fd_emit_page(h, b2, r2, nb2, prefw, wts, "Golden Wok" as *u8, pg2) 73 tot = tot + 1 74 var ok1: i64 = 1 75 if nb1 != nb2 { ok1 = 0 } 76 var t1i: i64 = 0; while t1i < nb1 { if b1[t1i] != b2[t1i] { ok1 = 0 } t1i = t1i + 1 } 77 if g_bytes_eq(pg1, pn1, pg2, pn2) == 0 { ok1 = 0 } 78 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 deterministic (identical build + identical " as *u8); g_i(pn1); g_p("-byte page on re-run)\n" as *u8) } else { g_p("FAIL T1 nondeterministic\n" as *u8) } 79 80 // -------- T2 role-complete (a real stir-fry) -------- 81 tot = tot + 1 82 var ok2: i64 = 1 83 if nb1 != 5 { ok2 = 0 } 84 if g_count_role(r1, nb1, FROLE_PRO) != 1 { ok2 = 0 } 85 if g_count_role(r1, nb1, FROLE_VEG) != 2 { ok2 = 0 } 86 if g_count_role(r1, nb1, FROLE_ARO) != 1 { ok2 = 0 } 87 if g_count_role(r1, nb1, FROLE_SAU) != 1 { ok2 = 0 } 88 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 role-complete: 1 protein, 2 veg, 1 aromatic, 1 sauce\n" as *u8) } else { g_p("FAIL T2 not role-complete\n" as *u8) } 89 90 // -------- T3 preference-responsive -------- 91 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 92 var a1: i64 = 0; while a1 < FOOD_NAX { prefw[a1] = 0; a1 = a1 + 1 } 93 prefw[4] = 3 // axis 4 = spicy: this eater LOVES spice 94 let ba: *i64 = sys_mmap(8 * 16) as *i64; let ra: *i64 = sys_mmap(8 * 16) as *i64 95 let nba: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, ba, ra) 96 let spicyA: i64 = fd_axis_total(h, ba, nba, 4) 97 var a2: i64 = 0; while a2 < FOOD_NAX { prefw[a2] = 0; a2 = a2 + 1 } 98 prefw[4] = 0 - 3; prefw[0] = 3 // dislikes spice, loves umami 99 let bbb: *i64 = sys_mmap(8 * 16) as *i64; let rb: *i64 = sys_mmap(8 * 16) as *i64 100 let nbb: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bbb, rb) 101 let spicyB: i64 = fd_axis_total(h, bbb, nbb, 4) 102 g_p("spice-lover pick: " as *u8); g_show(h, ba, nba) 103 g_p("spice-averse pick: " as *u8); g_show(h, bbb, nbb) 104 tot = tot + 1 105 if spicyA > spicyB { pass = pass + 1; g_p("PASS T3 preference-responsive (spicy total " as *u8); g_i(spicyA); g_p(" with spice-love vs " as *u8); g_i(spicyB); g_p(" with spice-aversion)\n" as *u8) } else { g_p("FAIL T3 not responsive (spicyA=" as *u8); g_i(spicyA); g_p(" spicyB=" as *u8); g_i(spicyB); g_p(")\n" as *u8) } 106 107 // -------- T4 availability-constrained -------- 108 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 109 var a3: i64 = 0; while a3 < FOOD_NAX { prefw[a3] = 0; a3 = a3 + 1 } 110 let bbase: *i64 = sys_mmap(8 * 16) as *i64; let rbase: *i64 = sys_mmap(8 * 16) as *i64 111 let nbbase: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bbase, rbase) 112 let exP: i64 = bbase[0] // the protein the engine WOULD choose 113 avail[exP] = 0 // this restaurant just ran out of it 114 let b4: *i64 = sys_mmap(8 * 16) as *i64; let r4: *i64 = sys_mmap(8 * 16) as *i64 115 let nb4: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b4, r4) 116 tot = tot + 1 117 var ok4: i64 = 1 118 if nb4 != 5 { ok4 = 0 } 119 if g_in_build(b4, nb4, exP) == 1 { ok4 = 0 } // the sold-out item must NOT reappear 120 var t4i: i64 = 0 121 while t4i < nb4 { if avail[b4[t4i]] == 0 { ok4 = 0 } t4i = t4i + 1 } // nothing unavailable in the build 122 avail[exP] = 1 123 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 availability-constrained (sold-out protein dropped, complete build rebuilt: " as *u8); g_show(h, b4, nb4) } else { g_p("FAIL T4\n" as *u8) } 124 125 // -------- T5 allergen hard-exclude -------- 126 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 127 let exS: i64 = bbase[4] // the sauce the engine WOULD choose 128 avoid[exS] = 1 // eater is allergic / hard-refuses it 129 let b5: *i64 = sys_mmap(8 * 16) as *i64; let r5: *i64 = sys_mmap(8 * 16) as *i64 130 let nb5: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b5, r5) 131 tot = tot + 1 132 var ok5: i64 = 1 133 if nb5 != 5 { ok5 = 0 } 134 if g_in_build(b5, nb5, exS) == 1 { ok5 = 0 } // the avoided item is NEVER served 135 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 allergen hard-exclude (avoided item kept off the plate, still complete)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) } 136 137 // -------- T6 negative control / liar-kill -------- 138 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 139 let role: *i64 = h[2] as *i64 140 var t6i: i64 = 0; while t6i < n { if role[t6i] == FROLE_PRO { avail[t6i] = 0 } t6i = t6i + 1 } // no protein on the menu 141 let b6: *i64 = sys_mmap(8 * 16) as *i64; let r6: *i64 = sys_mmap(8 * 16) as *i64 142 let nb6: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b6, r6) 143 fd_set_all(avail, n, 1) 144 let unk: i64 = fd_set_id(h, avail, "unicorn" as *u8, 1) // unknown id must be rejected LOUD (0) 145 tot = tot + 1 146 var ok6: i64 = 1 147 if nb6 != 0 { ok6 = 0 } // must NOT fabricate a stir-fry with no protein 148 if unk != 0 { ok6 = 0 } // must NOT silently accept a phantom ingredient 149 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 negative-control: no-protein -> honest INCOMPLETE (slots=" as *u8); g_i(nb6); g_p("), unknown id rejected\n" as *u8) } else { g_p("FAIL T6 (nb6=" as *u8); g_i(nb6); g_p(" unk=" as *u8); g_i(unk); g_p(")\n" as *u8) } 150 151 // -------- T7 MEASURED EXCEED (capability is the exceed) -------- 152 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0) 153 var a7: i64 = 0; while a7 < FOOD_NAX { prefw[a7] = 0; a7 = a7 + 1 } 154 let bo: *i64 = sys_mmap(8 * 16) as *i64; let ro: *i64 = sys_mmap(8 * 16) as *i64 155 let nbo: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bo, ro) 156 let bn: *i64 = sys_mmap(8 * 16) as *i64; let rn: *i64 = sys_mmap(8 * 16) as *i64 157 let nbn: i64 = fd_choose_naive(h, avail, avoid, frame, nframe, bn, rn) 158 let scoreO: i64 = fd_score(h, bo, nbo, prefw, wts) 159 let scoreN: i64 = fd_score(h, bn, nbn, prefw, wts) 160 g_p("science pick : " as *u8); g_show(h, bo, nbo) 161 g_p("naive pick : " as *u8); g_show(h, bn, nbn) 162 g_p(" score: science=" as *u8); g_i(scoreO); g_p(" naive=" as *u8); g_i(scoreN) 163 g_p(" | imbalance science=" as *u8); g_i(fd_imbalance(h, bo, nbo)); g_p(" naive=" as *u8); g_i(fd_imbalance(h, bn, nbn)) 164 g_p(" | affinity science=" as *u8); g_i(fd_affinity_total(h, bo, nbo)); g_p(" naive=" as *u8); g_i(fd_affinity_total(h, bn, nbn)); g_p("\n" as *u8) 165 tot = tot + 1 166 var ok7: i64 = 1 167 if nbn != 5 { ok7 = 0 } 168 if scoreO > scoreN { ok7 = ok7 } else { ok7 = 0 } 169 if ok7 == 1 { pass = pass + 1; g_p("PASS T7 measured exceed: the science build beats the naive strongest-ingredient build on the food-science objective\n" as *u8) } else { g_p("FAIL T7\n" as *u8) } 170 171 // -------- T8 sovereignty floor -------- 172 let page: *u8 = sys_mmap(32768) 173 let pgn: i64 = fd_emit_page(h, bo, ro, nbo, prefw, wts, "Golden Wok" as *u8, page) 174 let sfd: i64 = sys_openat_wr("knowledge/staging/food/page.html" as *u8, 420) 175 if sfd >= 0 { sys_write(sfd, page, pgn); sys_close(sfd) } 176 tot = tot + 1 177 if as_has_thirdparty_js(page, pgn) == 0 { pass = pass + 1; g_p("PASS T8 sovereignty: 0 third-party JS in the " as *u8); g_i(pgn); g_p("-byte recommendation page\n" as *u8) } else { g_p("FAIL T8 third-party JS leaked\n" as *u8) } 178 179 // -------- T9 teach-the-builder (the team grows) -------- 180 let cfd: i64 = sys_openat_wr(FG_CAT, 420); if cfd >= 0 { sys_close(cfd) } 181 nb_register(FG_CAT, "food" as *u8, "section" as *u8, "recommendation" as *u8, "Stir-fry recommendation" as *u8, "<section class='rec'><h2>{TITLE}</h2><div class='dish'>{DISH}</div><p class='why'>{WHY}</p></section>" as *u8) 182 nb_register(FG_CAT, "food" as *u8, "card" as *u8, "dish" as *u8, "Dish card" as *u8, "<article class='dish-card'><h3>{NAME}</h3><p>{ROLES}</p><p class='price'>{PRICE}</p></article>" as *u8) 183 nb_register(FG_CAT, "food" as *u8, "nav" as *u8, "foodbar" as *u8, "Food nav" as *u8, "<nav class='foodbar'><a class='brand' href='/'>{BRAND}</a><div>{LINKS}</div></nav>" as *u8) 184 let tpl: *u8 = sys_mmap(4096) 185 var ok9: i64 = 1 186 if nb_count(FG_CAT, "food" as *u8, "*" as *u8) < 3 { ok9 = 0 } 187 if nb_present(FG_CAT, "food" as *u8, "section" as *u8, "recommendation" as *u8) != 1 { ok9 = 0 } 188 if nb_lookup(FG_CAT, "food" as *u8, "section" as *u8, "recommendation" as *u8, tpl) == 1 { if nb_template_safe(tpl) != 1 { ok9 = 0 } } else { ok9 = 0 } 189 if nb_lookup(FG_CAT, "food" as *u8, "card" as *u8, "dish" as *u8, tpl) == 1 { if nb_template_safe(tpl) != 1 { ok9 = 0 } } else { ok9 = 0 } 190 tot = tot + 1 191 if ok9 == 1 { pass = pass + 1; g_p("PASS T9 taught the universal builder a 'food' domain (" as *u8); g_i(nb_count(FG_CAT, "food" as *u8, "*" as *u8)); g_p(" sovereign-safe emitters) -- build->teach->grow->consume\n" as *u8) } else { g_p("FAIL T9\n" as *u8) } 192 193 g_p("nx_food_stirfry_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 194 if pass == tot { g_p(" verdict=GREEN (Nishi food branch rung 0: the stir-fry chooser is LIVE + measured)\n" as *u8); return 0 } 195 g_p(" verdict=RED\n" as *u8) 196 return 1 197}