code wiki / _hdl_build / nx_food_ground_gate.nx

nx_food_ground_gate.nx source

↩ module page · 136 lines · 7922 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" 14 15const GR_DUR: *u8 = "knowledge/store/food-" 16const GR_TMP: *u8 = "/tmp/food_ing-" 17 18func 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 } 19func g_i(v: i64) -> i64 { 20 let bb: *u8 = sys_mmap(28); var m: i64 = v 21 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 22 let t: *u8 = sys_mmap(28); var k: i64 = 0 23 if m == 0 { t[0] = 48 as u8; k = 1 } 24 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 25 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 26} 27 28func main() -> i64 { 29 g_p("=== nx_food_ground_gate ===\n" as *u8) 30 31 var pass: i64 = 0 32 var tot: i64 = 0 33 34 // ---------- A. DURABLE GROUNDING (R0b) ---------- 35 let dwrote: i64 = fd_seed(GR_DUR) 36 let pwrote: i64 = fd_seed_provenance(GR_DUR) 37 g_p("durable store knowledge/store/food-: ingredients+config written=" as *u8); g_i(dwrote) 38 g_p(" provenance+science written=" as *u8); g_i(pwrote); g_p("\n" as *u8) 39 40 let h: *i64 = fd_world_open(GR_DUR) 41 let nIng: i64 = h[0] 42 let frame: *i64 = sys_mmap(8 * 16) as *i64 43 let nframe: i64 = fd_load_frame(GR_DUR, frame) 44 let wts: *i64 = sys_mmap(8 * 8) as *i64 45 fd_load_weights(GR_DUR, wts) 46 let avail: *i64 = sys_mmap(8 * 64) as *i64 47 let avoid: *i64 = sys_mmap(8 * 64) as *i64 48 let prefw: *i64 = sys_mmap(8 * 8) as *i64 49 fd_set_all(avail, nIng, 1); fd_set_all(avoid, nIng, 0) 50 var z: i64 = 0; while z < FOOD_NAX { prefw[z] = 0; z = z + 1 } 51 let bd: *i64 = sys_mmap(8 * 16) as *i64; let rd: *i64 = sys_mmap(8 * 16) as *i64 52 let nbd: i64 = fd_choose(h, avail, avoid, prefw, frame, nframe, wts, bd, rd) 53 tot = tot + 1 54 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) } 55 56 // ---------- T2 anti-bro-science: every science rule cites a real fetched source ---------- 57 let sciids: *u8 = sys_mmap(64) 58 var s1: i64 = 0; s1 = as_append(sciids, s1, "tastes\tumami\tpairing\tmaillard\twokhei\tmouthfeel\tpungency" as *u8); sciids[s1] = 0 as u8 59 let idbuf: *u8 = sys_mmap(48) 60 let cite: *u8 = sys_mmap(64) 61 var cited: i64 = 0 62 var total_sci: i64 = 0 63 var fcur: i64 = 0 64 while fcur >= 0 { 65 let l: i64 = fd_field(sciids, s1, fcur, idbuf) 66 if l == 0 { fcur = 0 - 1 } else { 67 total_sci = total_sci + 1 68 if fd_sci_cite_of(GR_DUR, idbuf, cite) == 1 { if fd_sci_cited(cite) == 1 { cited = cited + 1 } } 69 fcur = fcur + 1 70 } 71 } 72 tot = tot + 1 73 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) 74 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) } 75 76 // ---------- B. INGESTION GUARANTEES (hermetic) ---------- 77 fd_seed_provenance(GR_TMP) 78 79 // T3 admissible licenses pass 80 tot = tot + 1 81 var ok3: i64 = 1 82 if fd_source_admissible(GR_TMP, "PD" as *u8) != 1 { ok3 = 0 } 83 if fd_source_admissible(GR_TMP, "CCBYSA" as *u8) != 1 { ok3 = 0 } 84 if fd_source_admissible(GR_TMP, "FACTS" as *u8) != 1 { ok3 = 0 } 85 if fd_source_admissible(GR_TMP, "USGOV" as *u8) != 1 { ok3 = 0 } 86 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) } 87 88 // T4 copyrighted REFUSED (admissibility + actual ingest) 89 let ids: *i64 = sys_mmap(8 * 8) as *i64 90 ids[0] = "beef" as *u8 as i64; ids[1] = "broccoli" as *u8 as i64; ids[2] = "garlic" as *u8 as i64 91 let arr_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "ARR" as *u8) 92 tot = tot + 1 93 var ok4: i64 = 1 94 if fd_source_admissible(GR_TMP, "ARR" as *u8) != 0 { ok4 = 0 } 95 if arr_ingest != 0 { ok4 = 0 } 96 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) } 97 98 // T5 personal / flagged REFUSED 99 let ids2: *i64 = sys_mmap(8 * 8) as *i64 100 ids2[0] = "pork" as *u8 as i64; ids2[1] = "onion" as *u8 as i64 101 let pers_ingest: i64 = fd_ingest_recipe(GR_TMP, ids2, 2, "PERSONAL" as *u8) 102 tot = tot + 1 103 var ok5: i64 = 1 104 if fd_source_admissible(GR_TMP, "PERSONAL" as *u8) != 0 { ok5 = 0 } 105 if fd_source_admissible(GR_TMP, "FLAGGED" as *u8) != 0 { ok5 = 0 } 106 if pers_ingest != 0 { ok5 = 0 } 107 if fd_has_evidence(GR_TMP, "pork" as *u8, "onion" as *u8) != 0 { ok5 = 0 } // nothing written for a refused source 108 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) } 109 110 // T6 unknown license FAIL-CLOSED 111 let unk_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "BROSCIENCE" as *u8) 112 tot = tot + 1 113 var ok6: i64 = 1 114 if fd_source_admissible(GR_TMP, "BROSCIENCE" as *u8) != 0 { ok6 = 0 } 115 if unk_ingest != 0 { ok6 = 0 } 116 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) } 117 118 // T7 admissible ingest = FACTS ONLY (ingredient co-occurrence, no prose) 119 let facts_ingest: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "FACTS" as *u8) 120 tot = tot + 1 121 var ok7: i64 = 1 122 if facts_ingest != 3 { ok7 = 0 } // 3 ingredients -> 3 fact-pairs 123 if fd_has_evidence(GR_TMP, "beef" as *u8, "broccoli" as *u8) != 1 { ok7 = 0 } 124 if fd_has_evidence(GR_TMP, "beef" as *u8, "garlic" as *u8) != 1 { ok7 = 0 } 125 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) } 126 127 // T8 idempotent / additive re-ingest stays sound 128 let facts_ingest2: i64 = fd_ingest_recipe(GR_TMP, ids, 3, "FACTS" as *u8) 129 tot = tot + 1 130 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) } 131 132 g_p("nx_food_ground_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 133 if pass == tot { g_p(" verdict=GREEN (food branch GROUNDED + license-clean facts-only ingestion + cited science)\n" as *u8); return 0 } 134 g_p(" verdict=RED\n" as *u8) 135 return 1 136}