code wiki / _hdl_build / nx_garden_gate.nx
nx_garden_gate.nx source
↩ module page · 145 lines · 7762 B
1// nx_garden_gate.nx -- GATE: R-GARDEN. The grow-your-own affordability engine, grounded in the permaculture
2// research corpus. Proves: T1 growable model (veg/aromatics growable, proteins/processed sauces not), T2
3// buy-vs-grow savings (integer cents, no float), T3 research-grounded (crops CITE real perma_*.raw sources --
4// not bro science), T4 a cooked meal's growable ingredients carry real savings, T5 sovereign grow-plan render
5// with an annual projection, T6 SHIPPED via publisher. license_tier: ORIGINAL
6import "nx_garden.nx"
7import "nx_food_provenance.nx"
8import "nx_food_science.nx"
9import "nx_publisher.nx"
10import "nx_syscalls.nx"
11
12const GG_FOOD: *u8 = "knowledge/store/food-"
13const GG_Q: *u8 = "knowledge/publish/garden-queue.tsv"
14const GG_LED: *u8 = "knowledge/publish/garden-ledger.tsv"
15const GG_STG: *u8 = "knowledge/publish/garden-stage"
16const GG_LIVE: *u8 = "knowledge/publish/garden-live"
17const GG_STAGE_FILE: *u8 = "knowledge/staging/garden/plan.html"
18const GG_LIVE_FILE: *u8 = "knowledge/publish/garden-live/grow-plan.html"
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_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
30
31func main() -> i64 {
32 g_p("=== nx_garden_gate (R-GARDEN: grow-your-own affordability, grounded in the permaculture corpus) ===\n" as *u8)
33 fd_seed(GG_FOOD)
34 let cw: i64 = gd_seed(GG_FOOD)
35 g_p("garden crops authored this run=" as *u8); g_i(cw); g_p("\n" as *u8)
36
37 let world: *i64 = fd_world_open(GG_FOOD)
38 let names: *i64 = world[4] as *i64
39
40 var pass: i64 = 0
41 var tot: i64 = 0
42
43 // T1 growable model
44 tot = tot + 1
45 var ok1: i64 = 1
46 if gd_growable(GG_FOOD, "onion" as *u8) != 1 { ok1 = 0 }
47 if gd_growable(GG_FOOD, "garlic" as *u8) != 1 { ok1 = 0 }
48 if gd_growable(GG_FOOD, "beef" as *u8) != 0 { ok1 = 0 } // you don't grow beef in a garden
49 if gd_growable(GG_FOOD, "soy" as *u8) != 0 { ok1 = 0 } // processed sauce, not a crop
50 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 growable model (veg/aromatics grow, proteins/sauces don't)\n" as *u8) } else { g_p("FAIL T1\n" as *u8) }
51
52 // T2 buy-vs-grow savings
53 let sav_onion: i64 = gd_savings(GG_FOOD, "onion" as *u8)
54 let sav_garlic: i64 = gd_savings(GG_FOOD, "garlic" as *u8)
55 g_p("savings/unit: onion=" as *u8); g_i(sav_onion); g_p("c garlic=" as *u8); g_i(sav_garlic); g_p("c\n" as *u8)
56 tot = tot + 1
57 var ok2: i64 = 1
58 if sav_onion != 85 { ok2 = 0 } // buy 100 - grow 15
59 if sav_garlic != 88 { ok2 = 0 } // buy 100 - grow 12
60 if gd_savings(GG_FOOD, "beef" as *u8) != 0 { ok2 = 0 }
61 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 buy-vs-grow savings exact (onion 85c, garlic 88c per unit, no float)\n" as *u8) } else { g_p("FAIL T2\n" as *u8) }
62
63 // T3 research-grounded: every crop cites a real permaculture source
64 let pq: *i64 = sys_mmap(16) as *i64
65 let lq: *i64 = sys_mmap(16) as *i64
66 var grounded: i64 = 0
67 var ncrop: i64 = 0
68 if ss_get(GG_FOOD, "garden:cropids" as *u8, pq, lq) == 1 {
69 let buf: *u8 = pq[0] as *u8
70 let blen: i64 = lq[0]
71 var i: i64 = 0
72 var ls: i64 = 0
73 while i <= blen {
74 var sep: i64 = 0
75 if i == blen { sep = 1 } else { if buf[i] == (9 as u8) { sep = 1 } }
76 if sep == 1 {
77 let tl: i64 = i - ls
78 if tl > 0 {
79 let id: *u8 = sys_mmap(48)
80 var t: i64 = 0
81 while t < tl { id[t] = buf[ls + t]; t = t + 1 } id[tl] = 0 as u8
82 ncrop = ncrop + 1
83 let cite: *u8 = sys_mmap(64)
84 gd_field_str(GG_FOOD, id, 5, cite)
85 if fd_sci_cited(cite) == 1 { grounded = grounded + 1 }
86 }
87 ls = i + 1
88 }
89 i = i + 1
90 }
91 }
92 g_p("crops grounded in real permaculture sources: " as *u8); g_i(grounded); g_p("/" as *u8); g_i(ncrop); g_p("\n" as *u8)
93 tot = tot + 1
94 if grounded >= 10 { pass = pass + 1; g_p("PASS T3 research-grounded (>=10/15 crops cite a fetched permaculture source)\n" as *u8) } else { g_p("FAIL T3 only " as *u8); g_i(grounded); g_p(" grounded -- resume nx_perma_research_fetch\n" as *u8) }
95
96 // T4 a cooked meal's growable ingredients carry savings
97 let avail: *i64 = sys_mmap(8 * 64) as *i64
98 let avoid: *i64 = sys_mmap(8 * 64) as *i64
99 let prefw: *i64 = sys_mmap(8 * 8) as *i64
100 let n: i64 = world[0]
101 fd_set_all(avail, n, 1); fd_set_all(avoid, n, 0)
102 var z: i64 = 0; while z < FOOD_NAX { prefw[z] = 0; z = z + 1 }
103 let frame: *i64 = sys_mmap(8 * 16) as *i64; let nframe: i64 = fd_load_frame(GG_FOOD, frame)
104 let wts: *i64 = sys_mmap(8 * 8) as *i64; fd_load_weights(GG_FOOD, wts)
105 let build: *i64 = sys_mmap(8 * 16) as *i64; let roles: *i64 = sys_mmap(8 * 16) as *i64
106 let nb: i64 = fd_choose(world, avail, avoid, prefw, frame, nframe, wts, build, roles)
107 let og: *i64 = sys_mmap(16) as *i64
108 let mealsav: i64 = gd_meal_savings(GG_FOOD, world, build, nb, og)
109 g_p("cooked: " as *u8)
110 var si: i64 = 0; while si < nb { if si > 0 { g_p(" | " as *u8) } g_p(names[build[si]] as *u8); si = si + 1 }
111 g_p(" -> grow " as *u8); g_i(og[0]); g_p(" of them, save " as *u8); g_i(mealsav); g_p("c/meal\n" as *u8)
112 tot = tot + 1
113 var ok4: i64 = 1
114 if og[0] < 3 { ok4 = 0 }
115 if mealsav <= 0 { ok4 = 0 }
116 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 meal affordability (>=3 growable ingredients, real per-meal savings)\n" as *u8) } else { g_p("FAIL T4\n" as *u8) }
117
118 // T5 sovereign grow-plan render with an annual projection
119 let page: *u8 = sys_mmap(65536)
120 let np: i64 = gd_render_plan(GG_FOOD, world, build, nb, 3, page)
121 tot = tot + 1
122 var ok5: i64 = 1
123 if as_has_thirdparty_js(page, np) != 0 { ok5 = 0 }
124 if as_contains(page, np, "a year" as *u8) != 1 { ok5 = 0 }
125 if as_contains(page, np, "You save" as *u8) != 1 { ok5 = 0 }
126 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 sovereign grow-plan with annual savings projection (" as *u8); g_i(np); g_p(" bytes, 0 JS)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) }
127
128 // T6 ship
129 sys_mkdir("knowledge/staging" as *u8, 0x1ed)
130 sys_mkdir("knowledge/staging/garden" as *u8, 0x1ed)
131 let sfd: i64 = sys_openat_wr(GG_STAGE_FILE, 420); if sfd >= 0 { sys_write(sfd, page, np); sys_close(sfd) }
132 pub_init()
133 sys_mkdir(GG_STG, 0x1ed)
134 sys_mkdir(GG_LIVE, 0x1ed)
135 pub_submit_to(GG_Q, GG_STAGE_FILE, "grow-plan.html" as *u8, "nishifamily" as *u8, "nishi-garden" as *u8, "internal" as *u8)
136 let pubd: i64 = pub_run_full(GG_Q, GG_LED, GG_STG, GG_LIVE, "publish:garden" as *u8)
137 g_p(" pub_run_full -> published_this_pass=" as *u8); g_i(pubd); g_p("\n" as *u8)
138 tot = tot + 1
139 if g_exists(GG_LIVE_FILE) == 1 { pass = pass + 1; g_p("PASS T6 SHIPPED the grow-your-own plan to liveroot\n" as *u8) } else { g_p("FAIL T6\n" as *u8) }
140
141 g_p("nx_garden_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
142 if pass == tot { g_p(" verdict=GREEN (grow-your-own affordability, grounded in the permaculture corpus, shipped)\n" as *u8); return 0 }
143 g_p(" verdict=RED\n" as *u8)
144 return 1
145}