code wiki / _hdl_build / nx_food_stirfry_gate.nx
nx_food_stirfry_gate.nx source
↩ module page · 204 lines · 13836 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"
15import "nx_gate_verdict.nx"
16
17const FG_PREFIX: *u8 = "/tmp/food_g-"
18const FG_CAT: *u8 = "/tmp/nishi_food_gate.tsv"
19
20func 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 }
21func g_i(v: i64) -> i64 {
22 let bb: *u8 = sys_mmap(28); var m: i64 = v
23 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
24 let t: *u8 = sys_mmap(28); var k: i64 = 0
25 if m == 0 { t[0] = 48 as u8; k = 1 }
26 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
27 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
28}
29func g_name(h: *i64, idx: i64) -> i64 { let names: *i64 = h[4] as *i64; g_p(names[idx] as *u8); return 0 }
30func g_show(h: *i64, build: *i64, nb: i64) -> i64 {
31 var i: i64 = 0
32 while i < nb { if i > 0 { g_p(" | " as *u8) } g_name(h, build[i]); i = i + 1 }
33 g_p("\n" as *u8); return 0
34}
35func 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 }
36func 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 }
37func 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 }
38
39func main() -> i64 {
40 g_p("=== nx_food_stirfry_gate ===\n" as *u8)
41
42 let wrote: i64 = fd_seed(FG_PREFIX)
43 g_p("seeded food-science store (records written this run=" as *u8); g_i(wrote); g_p(")\n" as *u8)
44
45 let h: *i64 = fd_world_open(FG_PREFIX)
46 let n: i64 = h[0]
47 g_p("world: ingredients=" as *u8); g_i(n); g_p(" pairings=" as *u8); g_i(h[5]); g_p("\n" as *u8)
48 if n == 0 { g_p("FATAL: empty world (store seed/open failed)\nverdict=RED\n" as *u8); return 1 }
49
50 let frame: *i64 = sys_mmap(8 * 16) as *i64
51 let nframe: i64 = fd_load_frame(FG_PREFIX, frame)
52 let wts: *i64 = sys_mmap(8 * 8) as *i64
53 fd_load_weights(FG_PREFIX, wts)
54 g_p("frame slots=" as *u8); g_i(nframe); g_p(" weights[Krole,Wbal,Waff,Wpref,MAXIMB]=" as *u8)
55 var wi: i64 = 0; while wi < 5 { g_p(" " as *u8); g_i(wts[wi]); wi = wi + 1 } g_p("\n" as *u8)
56
57 let avail: *i64 = sys_mmap(8 * 64) as *i64
58 let avoid: *i64 = sys_mmap(8 * 64) as *i64
59 let prefw: *i64 = sys_mmap(8 * 8) as *i64
60
61 var pass: i64 = 0
62 var tot: i64 = 0
63
64 // -------- T1 determinism --------
65 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
66 var a0: i64 = 0; while a0 < FOOD_NAX { prefw[a0] = 0; a0 = a0 + 1 }
67 let b1: *i64 = sys_mmap(8 * 16) as *i64; let r1: *i64 = sys_mmap(8 * 16) as *i64
68 let b2: *i64 = sys_mmap(8 * 16) as *i64; let r2: *i64 = sys_mmap(8 * 16) as *i64
69 let nb1: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b1, r1)
70 let nb2: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b2, r2)
71 g_p("neutral pick: " as *u8); g_show(h, b1, nb1)
72 let pg1: *u8 = sys_mmap(32768); let pn1: i64 = fd_emit_page(h, b1, r1, nb1, prefw, wts, "Golden Wok" as *u8, pg1)
73 let pg2: *u8 = sys_mmap(32768); let pn2: i64 = fd_emit_page(h, b2, r2, nb2, prefw, wts, "Golden Wok" as *u8, pg2)
74 tot = tot + 1
75 var ok1: i64 = 1
76 if nb1 != nb2 { ok1 = 0 }
77 var t1i: i64 = 0; while t1i < nb1 { if b1[t1i] != b2[t1i] { ok1 = 0 } t1i = t1i + 1 }
78 if g_bytes_eq(pg1, pn1, pg2, pn2) == 0 { ok1 = 0 }
79 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) }
80
81 // -------- T2 role-complete (a real stir-fry) --------
82 tot = tot + 1
83 var ok2: i64 = 1
84 if nb1 != 5 { ok2 = 0 }
85 if g_count_role(r1, nb1, FROLE_PRO) != 1 { ok2 = 0 }
86 if g_count_role(r1, nb1, FROLE_VEG) != 2 { ok2 = 0 }
87 if g_count_role(r1, nb1, FROLE_ARO) != 1 { ok2 = 0 }
88 if g_count_role(r1, nb1, FROLE_SAU) != 1 { ok2 = 0 }
89 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) }
90
91 // -------- T3 preference-responsive --------
92 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
93 var a1: i64 = 0; while a1 < FOOD_NAX { prefw[a1] = 0; a1 = a1 + 1 }
94 prefw[4] = 3 // axis 4 = spicy: this eater LOVES spice
95 let ba: *i64 = sys_mmap(8 * 16) as *i64; let ra: *i64 = sys_mmap(8 * 16) as *i64
96 let nba: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, ba, ra)
97 let spicyA: i64 = fd_axis_total(h, ba, nba, 4)
98 var a2: i64 = 0; while a2 < FOOD_NAX { prefw[a2] = 0; a2 = a2 + 1 }
99 prefw[4] = 0 - 3; prefw[0] = 3 // dislikes spice, loves umami
100 let bbb: *i64 = sys_mmap(8 * 16) as *i64; let rb: *i64 = sys_mmap(8 * 16) as *i64
101 let nbb: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bbb, rb)
102 let spicyB: i64 = fd_axis_total(h, bbb, nbb, 4)
103 g_p("spice-lover pick: " as *u8); g_show(h, ba, nba)
104 g_p("spice-averse pick: " as *u8); g_show(h, bbb, nbb)
105 tot = tot + 1
106 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) }
107
108 // -------- T4 availability-constrained --------
109 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
110 var a3: i64 = 0; while a3 < FOOD_NAX { prefw[a3] = 0; a3 = a3 + 1 }
111 let bbase: *i64 = sys_mmap(8 * 16) as *i64; let rbase: *i64 = sys_mmap(8 * 16) as *i64
112 let nbbase: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bbase, rbase)
113 let exP: i64 = bbase[0] // the protein the engine WOULD choose
114 avail[exP] = 0 // this restaurant just ran out of it
115 let b4: *i64 = sys_mmap(8 * 16) as *i64; let r4: *i64 = sys_mmap(8 * 16) as *i64
116 let nb4: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b4, r4)
117 tot = tot + 1
118 var ok4: i64 = 1
119 if nb4 != 5 { ok4 = 0 }
120 if g_in_build(b4, nb4, exP) == 1 { ok4 = 0 } // the sold-out item must NOT reappear
121 var t4i: i64 = 0
122 while t4i < nb4 { if avail[b4[t4i]] == 0 { ok4 = 0 } t4i = t4i + 1 } // nothing unavailable in the build
123 avail[exP] = 1
124 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) }
125
126 // -------- T5 allergen hard-exclude --------
127 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
128 let exS: i64 = bbase[4] // the sauce the engine WOULD choose
129 avoid[exS] = 1 // eater is allergic / hard-refuses it
130 let b5: *i64 = sys_mmap(8 * 16) as *i64; let r5: *i64 = sys_mmap(8 * 16) as *i64
131 let nb5: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b5, r5)
132 tot = tot + 1
133 var ok5: i64 = 1
134 if nb5 != 5 { ok5 = 0 }
135 if g_in_build(b5, nb5, exS) == 1 { ok5 = 0 } // the avoided item is NEVER served
136 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) }
137
138 // -------- T6 negative control / liar-kill --------
139 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
140 let role: *i64 = h[2] as *i64
141 var t6i: i64 = 0; while t6i < n { if role[t6i] == FROLE_PRO { avail[t6i] = 0 } t6i = t6i + 1 } // no protein on the menu
142 let b6: *i64 = sys_mmap(8 * 16) as *i64; let r6: *i64 = sys_mmap(8 * 16) as *i64
143 let nb6: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, b6, r6)
144 fd_set_all(avail, n, 1)
145 let unk: i64 = fd_set_id(h, avail, "unicorn" as *u8, 1) // unknown id must be rejected LOUD (0)
146 tot = tot + 1
147 var ok6: i64 = 1
148 if nb6 != 0 { ok6 = 0 } // must NOT fabricate a stir-fry with no protein
149 if unk != 0 { ok6 = 0 } // must NOT silently accept a phantom ingredient
150 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) }
151
152 // -------- T7 MEASURED EXCEED (capability is the exceed) --------
153 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
154 var a7: i64 = 0; while a7 < FOOD_NAX { prefw[a7] = 0; a7 = a7 + 1 }
155 let bo: *i64 = sys_mmap(8 * 16) as *i64; let ro: *i64 = sys_mmap(8 * 16) as *i64
156 let nbo: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bo, ro)
157 let bn: *i64 = sys_mmap(8 * 16) as *i64; let rn: *i64 = sys_mmap(8 * 16) as *i64
158 let nbn: i64 = fd_choose_naive(h, avail, avoid, frame, nframe, bn, rn)
159 let scoreO: i64 = fd_score(h, bo, nbo, prefw, wts)
160 let scoreN: i64 = fd_score(h, bn, nbn, prefw, wts)
161 g_p("science pick : " as *u8); g_show(h, bo, nbo)
162 g_p("naive pick : " as *u8); g_show(h, bn, nbn)
163 g_p(" score: science=" as *u8); g_i(scoreO); g_p(" naive=" as *u8); g_i(scoreN)
164 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))
165 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)
166 tot = tot + 1
167 var ok7: i64 = 1
168 if nbn != 5 { ok7 = 0 }
169 if scoreO > scoreN { ok7 = ok7 } else { ok7 = 0 }
170 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) }
171
172 // -------- T8 sovereignty floor --------
173 let page: *u8 = sys_mmap(32768)
174 let pgn: i64 = fd_emit_page(h, bo, ro, nbo, prefw, wts, "Golden Wok" as *u8, page)
175 let sfd: i64 = sys_openat_wr("knowledge/staging/food/page.html" as *u8, 420)
176 if sfd >= 0 { sys_write(sfd, page, pgn); sys_close(sfd) }
177 tot = tot + 1
178 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) }
179
180 // -------- T9 teach-the-builder (the team grows) --------
181 let cfd: i64 = sys_openat_wr(FG_CAT, 420); if cfd >= 0 { sys_close(cfd) }
182 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)
183 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)
184 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)
185 let tpl: *u8 = sys_mmap(4096)
186 var ok9: i64 = 1
187 if nb_count(FG_CAT, "food" as *u8, "*" as *u8) < 3 { ok9 = 0 }
188 if nb_present(FG_CAT, "food" as *u8, "section" as *u8, "recommendation" as *u8) != 1 { ok9 = 0 }
189 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 }
190 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 }
191 tot = tot + 1
192 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) }
193
194 g_p("nx_food_stirfry_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
195 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
196 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
197 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
198 let ctr__dry: *i64 = gv_ctr()
199 ctr__dry[0] = pass
200 ctr__dry[1] = tot
201 let rc__dry: i64 = gv_verdict("FOOD-STIRFRY-GATE" as *u8, ctr__dry, "Nishi food branch rung 0: the stir-fry chooser is LIVE + measured)" as *u8)
202 sys_exit(rc__dry)
203 return rc__dry
204}