code wiki / _hdl_build / nx_link_gate.nx
nx_link_gate.nx source
↩ module page · 132 lines · 9153 B
1// nx_link_gate.nx -- GATE: the grand unifier (R-LINK). One canonical item (strawberry) resolves to ALL four
2// facets, each from its OWN sovereign store: T1 link registered/resolvable. T2 lineage (bio-). T3 clean/impact
3// (food-). T4 best local price (price-). T5 swap availability (swap-) + HONEST partial (an item with only
4// lineage shows the rest as "not recorded", never fabricated). T6 one unified sovereign view with all facets,
5// shipped. license_tier: ORIGINAL
6import "nx_link.nx"
7import "nx_publisher.nx"
8import "nx_seg_store.nx"
9import "nx_syscalls.nx"
10
11const LK_LINK: *u8 = "knowledge/store/link-"
12const LK_BIO: *u8 = "knowledge/store/lnk-bio-"
13const LK_FOOD: *u8 = "knowledge/store/lnk-food-"
14const LK_PRICE: *u8 = "knowledge/store/lnk-price-"
15const LK_SWAP: *u8 = "knowledge/store/lnk-swap-"
16const LK_AREA: *u8 = "TX-Austin"
17const LK_STAGE_FILE: *u8 = "knowledge/staging/link/strawberry.html"
18const LK_LIVE_FILE: *u8 = "knowledge/publish/link-live/strawberry.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_link_gate (R-LINK: one item, all its facets, one view) ===\n" as *u8)
33
34 // bio- : a strawberry lineage (founder -> F2, superior)
35 ln_seed_config(LK_BIO)
36 ln_add(LK_BIO, "straw_gp" as *u8, "Albion (founder)" as *u8, "plant" as *u8, "Albion" as *u8, 1, "-" as *u8, "-" as *u8, "saved" as *u8, "self" as *u8, "perma_a8_regenerative_ag" as *u8)
37 ln_add(LK_BIO, "straw_h" as *u8, "Albion (F2)" as *u8, "plant" as *u8, "Albion" as *u8, 2, "straw_gp" as *u8, "-" as *u8, "saved" as *u8, "self" as *u8, "perma_a8_regenerative_ag" as *u8)
38 ln_set_gene(LK_BIO, "straw_h" as *u8, 92, 88, 90, 85, 90, "Nishi field trial" as *u8, "perma_a8_regenerative_ag" as *u8)
39 ln_add(LK_BIO, "saff_org" as *u8, "Saffron heritage" as *u8, "plant" as *u8, "Saffron Crocus" as *u8, 1, "-" as *u8, "-" as *u8, "bought" as *u8, "spice farm" as *u8, "perma_a8_regenerative_ag" as *u8)
40
41 // food- : strawberry impact (conv vs nishi)
42 im_seed(LK_FOOD)
43
44 // price- : strawberry at two Austin stores
45 pr_add_store(LK_PRICE, "walmart_atx" as *u8, "Walmart Supercenter" as *u8, "Walmart" as *u8, "TX-Austin" as *u8, "TX" as *u8, "receipt" as *u8)
46 pr_add_store(LK_PRICE, "wf_atx" as *u8, "Whole Foods Market" as *u8, "Whole Foods" as *u8, "TX-Austin" as *u8, "TX" as *u8, "receipt" as *u8)
47 pr_set_price(LK_PRICE, "walmart_atx" as *u8, "strawberry" as *u8, 498, "2025-06-20" as *u8, "lb" as *u8, "receipt" as *u8)
48 pr_set_price(LK_PRICE, "wf_atx" as *u8, "strawberry" as *u8, 650, "2025-06-20" as *u8, "lb" as *u8, "receipt" as *u8)
49
50 // swap- : a neighbour gives strawberry runners
51 sw_seed_config(LK_SWAP)
52 sw_add_species(LK_SWAP, "strawberry" as *u8, "Strawberry" as *u8, "fruit" as *u8, "common" as *u8)
53 sw_add_gardener(LK_SWAP, "alice" as *u8, "Alice" as *u8, "TX-Austin" as *u8)
54 sw_post_offer(LK_SWAP, "a_straw" as *u8, "alice" as *u8, "strawberry" as *u8, "runner" as *u8, 6, "TX-Austin" as *u8, "-" as *u8)
55
56 // the LINKS
57 lk_register(LK_LINK, "strawberry" as *u8, "Strawberry" as *u8, "straw_h" as *u8, "strawberry" as *u8, "strawberry" as *u8, "strawberry" as *u8)
58 lk_register(LK_LINK, "saffron" as *u8, "Saffron" as *u8, "saff_org" as *u8, "-" as *u8, "-" as *u8, "-" as *u8)
59
60 var pass: i64 = 0
61 var tot: i64 = 0
62
63 // T1 link resolvable
64 let bid: *u8 = sys_mmap(48); lk_field(LK_LINK, "strawberry" as *u8, 1, bid)
65 let iid: *u8 = sys_mmap(48); lk_field(LK_LINK, "strawberry" as *u8, 2, iid)
66 g_p("link strawberry: bio=" as *u8); g_p(bid); g_p(" impact=" as *u8); g_p(iid); g_p("\n" as *u8)
67 tot = tot + 1
68 if fd_streq(bid, "straw_h" as *u8) == 1 { if fd_streq(iid, "strawberry" as *u8) == 1 { pass = pass + 1; g_p("PASS T1 link registered + resolvable (bio_id + impact_item)\n" as *u8) } else { g_p("FAIL T1 impact\n" as *u8) } } else { g_p("FAIL T1 bio\n" as *u8) }
69
70 // T2 lineage facet
71 let q: i64 = ln_quality_score(LK_BIO, "straw_h" as *u8)
72 g_p("lineage quality=" as *u8); g_i(q); g_p(" superior=" as *u8); g_i(ln_is_superior(LK_BIO, "straw_h" as *u8)); g_p("\n" as *u8)
73 tot = tot + 1
74 if q == 89 { if ln_is_superior(LK_BIO, "straw_h" as *u8) == 1 { pass = pass + 1; g_p("PASS T2 lineage facet resolves from bio- (quality 89, superior)\n" as *u8) } else { g_p("FAIL T2 superior\n" as *u8) } } else { g_p("FAIL T2 q=" as *u8); g_i(q); g_p("\n" as *u8) }
75
76 // T3 impact facet
77 let clean: i64 = im_clean(LK_FOOD, "strawberry" as *u8, "nishi" as *u8)
78 let save: i64 = im_savings(LK_FOOD, "strawberry" as *u8, "nishi" as *u8, "conv" as *u8)
79 g_p("clean=" as *u8); g_i(clean); g_p(" save=" as *u8); g_i(save); g_p("c\n" as *u8)
80 tot = tot + 1
81 if clean == 95 { if save == 150 { pass = pass + 1; g_p("PASS T3 impact facet resolves from food- (95/100 clean, save $1.50)\n" as *u8) } else { g_p("FAIL T3 save\n" as *u8) } } else { g_p("FAIL T3 clean\n" as *u8) }
82
83 // T4 price facet
84 let sid: *u8 = sys_mmap(48)
85 let cheap: i64 = pr_cheapest(LK_PRICE, LK_AREA, "strawberry" as *u8, sid)
86 g_p("cheapest=" as *u8); g_i(cheap); g_p(" at " as *u8); g_p(sid); g_p("\n" as *u8)
87 tot = tot + 1
88 if cheap == 498 { if fd_streq(sid, "walmart_atx" as *u8) == 1 { pass = pass + 1; g_p("PASS T4 price facet resolves from price- (cheapest $4.98 at Walmart)\n" as *u8) } else { g_p("FAIL T4 sid\n" as *u8) } } else { g_p("FAIL T4 cheap=" as *u8); g_i(cheap); g_p("\n" as *u8) }
89
90 // T5 swap facet + honest partial
91 let oids: *i64 = sys_mmap(8 * 64) as *i64
92 let noff: i64 = sw_find_offers(LK_SWAP, "strawberry" as *u8, LK_AREA, oids)
93 let saffpage: *u8 = sys_mmap(65536)
94 let snp: i64 = lk_render_item(LK_LINK, LK_BIO, LK_FOOD, LK_PRICE, LK_SWAP, "saffron" as *u8, LK_AREA, saffpage)
95 g_p("strawberry offers=" as *u8); g_i(noff); g_p(" | saffron(partial) page=" as *u8); g_i(snp); g_p(" bytes\n" as *u8)
96 tot = tot + 1
97 var ok5: i64 = 1
98 if noff != 1 { ok5 = 0 }
99 if as_contains(saffpage, snp, "Saffron Crocus" as *u8) != 1 { ok5 = 0 } // lineage present
100 if as_contains(saffpage, snp, "No impact data" as *u8) != 1 { ok5 = 0 } // absent facet handled honestly
101 if as_contains(saffpage, snp, "No price data" as *u8) != 1 { ok5 = 0 }
102 if as_contains(saffpage, snp, "Not in the swap network" as *u8) != 1 { ok5 = 0 }
103 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 swap facet resolves (1 offer) + partial item shows present facet, others 'not recorded' (honest)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) }
104
105 // T6 the unified view: all four facets in one sovereign page
106 let page: *u8 = sys_mmap(65536)
107 let np: i64 = lk_render_item(LK_LINK, LK_BIO, LK_FOOD, LK_PRICE, LK_SWAP, "strawberry" as *u8, LK_AREA, page)
108 g_p("unified strawberry view = " as *u8); g_i(np); g_p(" bytes\n" as *u8)
109 tot = tot + 1
110 var ok6: i64 = 1
111 if as_has_thirdparty_js(page, np) != 0 { ok6 = 0 }
112 if as_contains(page, np, "Albion" as *u8) != 1 { ok6 = 0 } // lineage
113 if as_contains(page, np, "superior strain" as *u8) != 1 { ok6 = 0 }
114 if as_contains(page, np, "95/100 clean" as *u8) != 1 { ok6 = 0 } // impact
115 if as_contains(page, np, "$4.98" as *u8) != 1 { ok6 = 0 } // price
116 if as_contains(page, np, "1 neighbour" as *u8) != 1 { ok6 = 0 } // swap
117 sys_mkdir("knowledge/staging" as *u8, 0x1ed)
118 sys_mkdir("knowledge/staging/link" as *u8, 0x1ed)
119 let sfd: i64 = sys_openat_wr(LK_STAGE_FILE, 420); if sfd >= 0 { sys_write(sfd, page, np); sys_close(sfd) }
120 pub_init()
121 sys_mkdir("knowledge/publish/link-stage" as *u8, 0x1ed)
122 sys_mkdir("knowledge/publish/link-live" as *u8, 0x1ed)
123 pub_submit_to("knowledge/publish/link-queue.tsv" as *u8, LK_STAGE_FILE, "strawberry.html" as *u8, "nishifoodfamily" as *u8, "nishi-link" as *u8, "internal" as *u8)
124 pub_run_full("knowledge/publish/link-queue.tsv" as *u8, "knowledge/publish/link-ledger.tsv" as *u8, "knowledge/publish/link-stage" as *u8, "knowledge/publish/link-live" as *u8, "publish:link" as *u8)
125 if g_exists(LK_LIVE_FILE) != 1 { ok6 = 0 }
126 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 ONE unified view: lineage + clean + price + swap for a strawberry, sovereign, shipped\n" as *u8) } else { g_p("FAIL T6\n" as *u8) }
127
128 g_p("nx_link_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
129 if pass == tot { g_p(" verdict=GREEN (one strawberry: its lineage, cleanliness, best price, and swap availability, together)\n" as *u8); return 0 }
130 g_p(" verdict=RED\n" as *u8)
131 return 1
132}