code wiki / _hdl_build / nx_food_ground_gate.nx

nx_food_ground_gate.nx source

↩ module page · 143 lines · 8322 B

1// nx_food_ground_gate.nx -- GATE: grounds the Nishi food branch (R0b) and proves the recipe-ingestion 2// guarantees the operator asked for ("ingest recipes that aren't copyrighted or flagged to a person ... uses 3// research not bro science"). Two parts: 4// A. DURABLE GROUNDING -- seeds the live store knowledge/store/food- (ingredients + licenses + cited 5// science), opens it, and proves the chooser is role-complete on the durable branch. 6// B. INGESTION GUARANTEES (hermetic /tmp) -- admissible sources (PD/CC/FACTS/US-Gov) ingest as FACTS ONLY; 7// copyrighted (ARR), personal-flagged, and UNKNOWN licenses are REFUSED (fail-closed); every science 8// rule CITES a real fetched research source (anti-bro-science). 9// license_tier: ORIGINAL 10import "nx_food_provenance.nx" 11import "nx_food_science.nx" 12import "nx_seg_store.nx" 13import "nx_syscalls.nx" 14import "nx_gate_verdict.nx" 15 16const GR_DUR: *u8 = "knowledge/store/food-" 17const GR_TMP: *u8 = "/tmp/food_ing-" 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} 28 29func main() -> i64 { 30 g_p("=== nx_food_ground_gate ===\n" as *u8) 31 32 var pass: i64 = 0 33 var tot: i64 = 0 34 35 // ---------- A. DURABLE GROUNDING (R0b) ---------- 36 let dwrote: i64 = fd_seed(GR_DUR) 37 let pwrote: i64 = fd_seed_provenance(GR_DUR) 38 g_p("durable store knowledge/store/food-: ingredients+config written=" as *u8); g_i(dwrote) 39 g_p(" provenance+science written=" as *u8); g_i(pwrote); g_p("\n" as *u8) 40 41 let h: *i64 = fd_world_open(GR_DUR) 42 let nIng: i64 = h[0] 43 let frame: *i64 = sys_mmap(8 * 16) as *i64 44 let nframe: i64 = fd_load_frame(GR_DUR, frame) 45 let wts: *i64 = sys_mmap(8 * 8) as *i64 46 fd_load_weights(GR_DUR, wts) 47 let avail: *i64 = sys_mmap(8 * 64) as *i64 48 let avoid: *i64 = sys_mmap(8 * 64) as *i64 49 let prefw: *i64 = sys_mmap(8 * 8) as *i64 50 fd_set_all(avail, nIng, 1); fd_set_all(avoid, nIng, 0) 51 var z: i64 = 0; while z < FOOD_NAX { prefw[z] = 0; z = z + 1 } 52 let bd: *i64 = sys_mmap(8 * 16) as *i64; let rd: *i64 = sys_mmap(8 * 16) as *i64 53 let nbd: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bd, rd) 54 tot = tot + 1 55 if nIng == 26 { if nbd == 5 { pass = pass + 1; g_p("PASS T1 durable branch grounded: " as *u8); g_i(nIng); g_p(" ingredients live, chooser role-complete on knowledge/store/food-\n" as *u8) } else { g_p("FAIL T1 chooser not complete on durable store\n" as *u8) } } else { g_p("FAIL T1 durable ingredient count=" as *u8); g_i(nIng); g_p("\n" as *u8) } 56 57 // ---------- T2 anti-bro-science: every science rule cites a real fetched source ---------- 58 let sciids: *u8 = sys_mmap(64) 59 var s1: i64 = 0; s1 = as_append(sciids, s1, "tastes\tumami\tpairing\tmaillard\twokhei\tmouthfeel\tpungency" as *u8); sciids[s1] = 0 as u8 60 let idbuf: *u8 = sys_mmap(48) 61 let cite: *u8 = sys_mmap(64) 62 var cited: i64 = 0 63 var total_sci: i64 = 0 64 var fcur: i64 = 0 65 while fcur >= 0 { 66 let l: i64 = fd_field(sciids, s1, fcur, idbuf) 67 if l == 0 { fcur = 0 - 1 } else { 68 total_sci = total_sci + 1 69 if fd_sci_cite_of(GR_DUR, idbuf, cite) == 1 { if fd_sci_cited(cite) == 1 { cited = cited + 1 } } 70 fcur = fcur + 1 71 } 72 } 73 tot = tot + 1 74 g_p("science rules cited to real fetched sources: " as *u8); g_i(cited); g_p("/" as *u8); g_i(total_sci); g_p("\n" as *u8) 75 if cited == total_sci { if total_sci > 0 { pass = pass + 1; g_p("PASS T2 anti-bro-science: every food-science rule is grounded in a fetched research source\n" as *u8) } else { g_p("FAIL T2 no science rules\n" as *u8) } } else { g_p("FAIL T2 ungrounded rules present\n" as *u8) } 76 77 // ---------- B. INGESTION GUARANTEES (hermetic) ---------- 78 fd_seed_provenance(GR_TMP) 79 80 // T3 admissible licenses pass 81 tot = tot + 1 82 var ok3: i64 = 1 83 if fd_source_admissible(GR_TMP, "PD" as *u8) != 1 { ok3 = 0 } 84 if fd_source_admissible(GR_TMP, "CCBYSA" as *u8) != 1 { ok3 = 0 } 85 if fd_source_admissible(GR_TMP, "FACTS" as *u8) != 1 { ok3 = 0 } 86 if fd_source_admissible(GR_TMP, "USGOV" as *u8) != 1 { ok3 = 0 } 87 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 admissible licenses accepted (PD, CC-BY-SA, FACTS, US-Gov)\n" as *u8) } else { g_p("FAIL T3\n" as *u8) } 88 89 // T4 copyrighted REFUSED (admissibility + actual ingest) 90 let ids: *i64 = sys_mmap(8 * 8) as *i64 91 ids[0] = "beef" as *u8 as i64; ids[1] = "broccoli" as *u8 as i64; ids[2] = "garlic" as *u8 as i64 92 let arr_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "ARR" as *u8) 93 tot = tot + 1 94 var ok4: i64 = 1 95 if fd_source_admissible(GR_TMP, "ARR" as *u8) != 0 { ok4 = 0 } 96 if arr_ingest != 0 { ok4 = 0 } 97 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 copyrighted source REFUSED (all-rights-reserved -> 0 ingested)\n" as *u8) } else { g_p("FAIL T4\n" as *u8) } 98 99 // T5 personal / flagged REFUSED 100 let ids2: *i64 = sys_mmap(8 * 8) as *i64 101 ids2[0] = "pork" as *u8 as i64; ids2[1] = "onion" as *u8 as i64 102 let pers_ingest: i64 = fd_ingest_recipe(GR_TMP, ids2, 2, "PERSONAL" as *u8) 103 tot = tot + 1 104 var ok5: i64 = 1 105 if fd_source_admissible(GR_TMP, "PERSONAL" as *u8) != 0 { ok5 = 0 } 106 if fd_source_admissible(GR_TMP, "FLAGGED" as *u8) != 0 { ok5 = 0 } 107 if pers_ingest != 0 { ok5 = 0 } 108 if fd_has_evidence(GR_TMP, "pork" as *u8, "onion" as *u8) != 0 { ok5 = 0 } // nothing written for a refused source 109 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 personal/flagged source REFUSED (nothing ingested, nothing stored)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) } 110 111 // T6 unknown license FAIL-CLOSED 112 let unk_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "BROSCIENCE" as *u8) 113 tot = tot + 1 114 var ok6: i64 = 1 115 if fd_source_admissible(GR_TMP, "BROSCIENCE" as *u8) != 0 { ok6 = 0 } 116 if unk_ingest != 0 { ok6 = 0 } 117 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 unknown license fail-closed (unrecognised -> REFUSED, not assumed safe)\n" as *u8) } else { g_p("FAIL T6\n" as *u8) } 118 119 // T7 admissible ingest = FACTS ONLY (ingredient co-occurrence, no prose) 120 let facts_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "FACTS" as *u8) 121 tot = tot + 1 122 var ok7: i64 = 1 123 if facts_ingest != 3 { ok7 = 0 } // 3 ingredients -> 3 fact-pairs 124 if fd_has_evidence(GR_TMP, "beef" as *u8, "broccoli" as *u8) != 1 { ok7 = 0 } 125 if fd_has_evidence(GR_TMP, "beef" as *u8, "garlic" as *u8) != 1 { ok7 = 0 } 126 if ok7 == 1 { pass = pass + 1; g_p("PASS T7 facts-only ingestion: admissible recipe -> " as *u8); g_i(facts_ingest); g_p(" ingredient-pair FACTS recorded (no prose)\n" as *u8) } else { g_p("FAIL T7 (ingested=" as *u8); g_i(facts_ingest); g_p(")\n" as *u8) } 127 128 // T8 idempotent / additive re-ingest stays sound 129 let facts_ingest2: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "FACTS" as *u8) 130 tot = tot + 1 131 if facts_ingest2 == 3 { if fd_has_evidence(GR_TMP, "broccoli" as *u8, "garlic" as *u8) == 1 { pass = pass + 1; g_p("PASS T8 additive re-ingest sound (evidence accumulates, store stays readable)\n" as *u8) } else { g_p("FAIL T8 evidence missing\n" as *u8) } } else { g_p("FAIL T8 re-ingest=" as *u8); g_i(facts_ingest2); g_p("\n" as *u8) } 132 133 g_p("nx_food_ground_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 134 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 135 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 136 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 137 let ctr__dry: *i64 = gv_ctr() 138 ctr__dry[0] = pass 139 ctr__dry[1] = tot 140 let rc__dry: i64 = gv_verdict("FOOD-GROUND-GATE" as *u8, ctr__dry, "food branch GROUNDED + license-clean facts-only ingestion + cited science)" as *u8) 141 sys_exit(rc__dry) 142 return rc__dry 143}