code wiki / _hdl_build / nx_food_provenance.nx
nx_food_provenance.nx source
↩ module page · 157 lines · 8360 B
1// nx_food_provenance.nx -- LIB: the LICENSE / PROVENANCE + ANTI-BRO-SCIENCE layer for the Nishi food branch
2// (operator: "ingest recipes that aren't copyrighted or otherwise flagged to a person ... uses research not
3// bro science"). Three guarantees, all data-driven (seg-store, no TSV) and FAIL-CLOSED by construction:
4// 1. ADMISSIBILITY GATE -- fd_source_admissible: only explicitly-admissible licenses pass (PD, CC0, CC-BY,
5// CC-BY-SA, ODbL, US-Gov, or pure non-copyrightable FACTS = ingredient lists/ratios). Copyrighted (ARR),
6// personal/flagged, AND unknown licenses are REFUSED. Recipe FACTS are not copyrightable (17 USC 102(b));
7// creative prose is -- so we ingest the facts, never the prose, never a person's flagged data.
8// 2. FACTS-ONLY INGESTION -- fd_ingest_recipe: writes ingredient co-occurrence facts (food:eob:<a>:<b>),
9// never names/headnotes/prose. Refuses inadmissible sources LOUD (returns 0).
10// 3. ANTI-BRO-SCIENCE -- fd_sci_cited: every food-science rule (food:sci:<id>) carries a CITE naming a real
11// fetched research source (knowledge/fetched/<cite>.raw); a rule whose source is absent is NOT grounded.
12// Reuses nx_food_science (fd_field/fd_atoi/fd_seg_next/fd_streq_store + ss_*/as_*). license_tier: ORIGINAL
13import "nx_food_science.nx"
14import "nx_seg_store.nx"
15import "nx_syscalls.nx"
16
17// 1 if the license `code` is explicitly admissible in the store; 0 otherwise (unknown code => 0 = FAIL-CLOSED).
18func fd_source_admissible(prefix: *u8, code: *u8) -> i64 {
19 let key: *u8 = sys_mmap(80)
20 var o: i64 = 0
21 o = as_append(key, o, "food:lic:" as *u8)
22 o = as_append(key, o, code)
23 key[o] = 0 as u8
24 let pq: *i64 = sys_mmap(16) as *i64
25 let lq: *i64 = sys_mmap(16) as *i64
26 if ss_get(prefix, key, pq, lq) != 1 { return 0 }
27 let rec: *u8 = pq[0] as *u8
28 let rl: i64 = lq[0]
29 let fb: *u8 = sys_mmap(16)
30 let l: i64 = fd_field(rec, rl, 1, fb) // field 1 = admissible flag (0/1)
31 return fd_atoi(fb, l)
32}
33
34// 1 if the research source `cite` (a fetched-file basename) actually exists at knowledge/fetched/<cite>.raw.
35// This is the anti-bro-science check: a science rule is grounded only if its cited source is really present.
36func fd_sci_cited(cite: *u8) -> i64 {
37 let path: *u8 = sys_mmap(160)
38 var o: i64 = 0
39 o = as_append(path, o, "knowledge/fetched/" as *u8)
40 o = as_append(path, o, cite)
41 o = as_append(path, o, ".raw" as *u8)
42 path[o] = 0 as u8
43 let fd: i64 = sys_openat_rd(path)
44 if fd < 0 { return 0 }
45 sys_close(fd)
46 return 1
47}
48
49// read the CITE field (field 1) of food:sci:<id> into out; returns 1 if the rule exists.
50func fd_sci_cite_of(prefix: *u8, id: *u8, out: *u8) -> i64 {
51 let key: *u8 = sys_mmap(80)
52 var o: i64 = 0
53 o = as_append(key, o, "food:sci:" as *u8)
54 o = as_append(key, o, id)
55 key[o] = 0 as u8
56 let pq: *i64 = sys_mmap(16) as *i64
57 let lq: *i64 = sys_mmap(16) as *i64
58 if ss_get(prefix, key, pq, lq) != 1 { return 0 }
59 fd_field(pq[0] as *u8, lq[0], 1, out)
60 return 1
61}
62
63// INGEST a recipe as FACTS ONLY: if the license is admissible, record ingredient co-occurrence evidence
64// (food:eob:<idA>:<idB>) for every pair -- never prose. Returns the number of fact-pairs recorded, or 0 if
65// the source is refused (copyrighted / personal / unknown). `ids` is an array of NUL-terminated id strings.
66func fd_ingest_recipe(prefix: *u8, ids: *i64, nids: i64, code: *u8) -> i64 {
67 if fd_source_admissible(prefix, code) == 0 { return 0 }
68 let w: *i64 = ss_begin()
69 var added: i64 = 0
70 var i: i64 = 0
71 while i < nids {
72 var j: i64 = i + 1
73 while j < nids {
74 let key: *u8 = sys_mmap(96)
75 var o: i64 = 0
76 o = as_append(key, o, "food:eob:" as *u8)
77 o = as_append(key, o, ids[i] as *u8)
78 key[o] = 58 as u8; o = o + 1 // ':'
79 o = as_append(key, o, ids[j] as *u8)
80 key[o] = 0 as u8
81 ss_add(w, 1, key, "1" as *u8, 1) // presence-of-evidence; additive versions accumulate
82 added = added + 1
83 j = j + 1
84 }
85 i = i + 1
86 }
87 if added > 0 {
88 let segid: i64 = fd_seg_next(prefix)
89 ss_commit(prefix, w, segid)
90 }
91 return added
92}
93
94// 1 if a co-occurrence fact food:eob:<a>:<b> is present in the store
95func fd_has_evidence(prefix: *u8, a: *u8, b: *u8) -> i64 {
96 let key: *u8 = sys_mmap(96)
97 var o: i64 = 0
98 o = as_append(key, o, "food:eob:" as *u8)
99 o = as_append(key, o, a)
100 key[o] = 58 as u8; o = o + 1
101 o = as_append(key, o, b)
102 key[o] = 0 as u8
103 let pq: *i64 = sys_mmap(16) as *i64
104 let lq: *i64 = sys_mmap(16) as *i64
105 if ss_get(prefix, key, pq, lq) == 1 { return 1 }
106 return 0
107}
108
109// ---- seed the license table + the cited science rules (idempotent / additive) -------------------------
110// food:lic:<code> -> label <t> admissible(0/1)
111// food:sci:<id> -> claim <t> cite(fetched-file basename, no .raw)
112func fd_seed_provenance(prefix: *u8) -> i64 {
113 let keys: *i64 = sys_mmap(8 * 48) as *i64
114 let vals: *i64 = sys_mmap(8 * 48) as *i64
115 var n: i64 = 0
116
117 // license admissibility table -- the data-driven legal floor
118 keys[n] = "food:licids" as *u8 as i64
119 vals[n] = "PD\tCC0\tCCBY\tCCBYSA\tODBL\tUSGOV\tFACTS\tARR\tPERSONAL\tFLAGGED" as *u8 as i64; n = n + 1
120 keys[n] = "food:lic:PD" as *u8 as i64; vals[n] = "Public domain\t1" as *u8 as i64; n = n + 1
121 keys[n] = "food:lic:CC0" as *u8 as i64; vals[n] = "CC0 no rights reserved\t1" as *u8 as i64; n = n + 1
122 keys[n] = "food:lic:CCBY" as *u8 as i64; vals[n] = "CC BY\t1" as *u8 as i64; n = n + 1
123 keys[n] = "food:lic:CCBYSA" as *u8 as i64; vals[n] = "CC BY-SA (Wikipedia)\t1" as *u8 as i64; n = n + 1
124 keys[n] = "food:lic:ODBL" as *u8 as i64; vals[n] = "Open Database License\t1" as *u8 as i64; n = n + 1
125 keys[n] = "food:lic:USGOV" as *u8 as i64; vals[n] = "US Government public domain (USDA)\t1" as *u8 as i64; n = n + 1
126 keys[n] = "food:lic:FACTS" as *u8 as i64; vals[n] = "Non-copyrightable facts: ingredient lists/ratios\t1" as *u8 as i64; n = n + 1
127 keys[n] = "food:lic:ARR" as *u8 as i64; vals[n] = "All rights reserved (copyrighted prose)\t0" as *u8 as i64; n = n + 1
128 keys[n] = "food:lic:PERSONAL" as *u8 as i64; vals[n] = "Personal/private data flagged to a person\t0" as *u8 as i64; n = n + 1
129 keys[n] = "food:lic:FLAGGED" as *u8 as i64; vals[n] = "Flagged/restricted source\t0" as *u8 as i64; n = n + 1
130
131 // cited science rules -- each CITE names a fetched research source (anti-bro-science)
132 keys[n] = "food:sciids" as *u8 as i64
133 vals[n] = "tastes\tumami\tpairing\tmaillard\twokhei\tmouthfeel\tpungency" as *u8 as i64; n = n + 1
134 keys[n] = "food:sci:tastes" as *u8 as i64; vals[n] = "Five basic tastes: sweet sour salty bitter umami\tfood_s2_basic_taste" as *u8 as i64; n = n + 1
135 keys[n] = "food:sci:umami" as *u8 as i64; vals[n] = "Umami = glutamate, synergy with 5-prime nucleotides\tfood_s3_umami" as *u8 as i64; n = n + 1
136 keys[n] = "food:sci:pairing" as *u8 as i64; vals[n] = "Food-pairing hypothesis: shared aroma compounds combine well\tfood_p1_foodpairing" as *u8 as i64; n = n + 1
137 keys[n] = "food:sci:maillard" as *u8 as i64; vals[n] = "Maillard reaction builds savory browning + aroma at high heat\tfood_t4_maillard" as *u8 as i64; n = n + 1
138 keys[n] = "food:sci:wokhei" as *u8 as i64; vals[n] = "Wok hei: high-heat wok aroma from stir-frying\tfood_t2_wok" as *u8 as i64; n = n + 1
139 keys[n] = "food:sci:mouthfeel" as *u8 as i64; vals[n] = "Mouthfeel / texture contrast is part of a balanced dish\tfood_s6_mouthfeel" as *u8 as i64; n = n + 1
140 keys[n] = "food:sci:pungency" as *u8 as i64; vals[n] = "Pungency (capsaicin) is trigeminal heat, not a basic taste\tfood_s7_pungency" as *u8 as i64; n = n + 1
141
142 let w: *i64 = ss_begin()
143 var towrite: i64 = 0
144 var i: i64 = 0
145 while i < n {
146 let key: *u8 = keys[i] as *u8
147 let val: *u8 = vals[i] as *u8
148 let vl: i64 = as_len(val)
149 if fd_streq_store(prefix, key, val, vl) == 0 { ss_add(w, 1, key, val, vl); towrite = towrite + 1 }
150 i = i + 1
151 }
152 if towrite > 0 {
153 let segid: i64 = fd_seg_next(prefix)
154 ss_commit(prefix, w, segid)
155 }
156 return towrite
157}