code wiki / _hdl_build / nx_price_gate.nx

nx_price_gate.nx source

↩ module page · 162 lines · 10404 B

1// nx_price_gate.nx -- GATE: price tracking R-PRICE. T1 stores in areas across America (area isolation). T2 price 2// observations recorded WITH provenance (date + source). T3 cheapest store + area median for an item. T4 "how a 3// store is doing" = competitiveness index vs area median (discount <100, premium >100). T5 basket totals + 4// cheapest basket, and another area's prices DON'T leak into this area's median. T6 the area price page renders 5// sovereign, cheapest-first, with the honest "recorded observations" note + ships. license_tier: ORIGINAL 6import "nx_price.nx" 7import "nx_publisher.nx" 8import "nx_seg_store.nx" 9import "nx_syscalls.nx" 10 11const PG_STORE: *u8 = "knowledge/store/price-" 12const PG_STAGE_FILE: *u8 = "knowledge/staging/price/austin.html" 13const PG_LIVE_FILE: *u8 = "knowledge/publish/price-live/austin.html" 14 15func 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 } 16func g_i(v: i64) -> i64 { 17 let bb: *u8 = sys_mmap(28); var m: i64 = v 18 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 19 let t: *u8 = sys_mmap(28); var k: i64 = 0 20 if m == 0 { t[0] = 48 as u8; k = 1 } 21 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 22 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 23} 24func g_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 } 25 26func main() -> i64 { 27 g_p("=== nx_price_gate (R-PRICE: price tracking across US stores) ===\n" as *u8) 28 29 // stores across two areas (proves "across america" + area isolation) 30 pr_add_store(PG_STORE, "walmart_atx" as *u8, "Walmart Supercenter" as *u8, "Walmart" as *u8, "TX-Austin" as *u8, "TX" as *u8, "receipt" as *u8) 31 pr_add_store(PG_STORE, "heb_atx" as *u8, "H-E-B" as *u8, "H-E-B" as *u8, "TX-Austin" as *u8, "TX" as *u8, "receipt" as *u8) 32 pr_add_store(PG_STORE, "wf_atx" as *u8, "Whole Foods Market" as *u8, "Whole Foods" as *u8, "TX-Austin" as *u8, "TX" as *u8, "receipt" as *u8) 33 pr_add_store(PG_STORE, "ralphs_la" as *u8, "Ralphs" as *u8, "Ralphs" as *u8, "CA-LA" as *u8, "CA" as *u8, "receipt" as *u8) 34 35 // price observations (cents) -- milk, eggs, bread 36 pr_set_price(PG_STORE, "walmart_atx" as *u8, "milk" as *u8, 298, "2025-06-20" as *u8, "gal" as *u8, "receipt" as *u8) 37 pr_set_price(PG_STORE, "walmart_atx" as *u8, "eggs" as *u8, 240, "2025-06-20" as *u8, "dozen" as *u8, "receipt" as *u8) 38 pr_set_price(PG_STORE, "walmart_atx" as *u8, "bread" as *u8, 148, "2025-06-20" as *u8, "loaf" as *u8, "receipt" as *u8) 39 pr_set_price(PG_STORE, "heb_atx" as *u8, "milk" as *u8, 320, "2025-06-20" as *u8, "gal" as *u8, "receipt" as *u8) 40 pr_set_price(PG_STORE, "heb_atx" as *u8, "eggs" as *u8, 265, "2025-06-20" as *u8, "dozen" as *u8, "receipt" as *u8) 41 pr_set_price(PG_STORE, "heb_atx" as *u8, "bread" as *u8, 175, "2025-06-20" as *u8, "loaf" as *u8, "receipt" as *u8) 42 pr_set_price(PG_STORE, "wf_atx" as *u8, "milk" as *u8, 420, "2025-06-20" as *u8, "gal" as *u8, "receipt" as *u8) 43 pr_set_price(PG_STORE, "wf_atx" as *u8, "eggs" as *u8, 380, "2025-06-20" as *u8, "dozen" as *u8, "receipt" as *u8) 44 pr_set_price(PG_STORE, "wf_atx" as *u8, "bread" as *u8, 320, "2025-06-20" as *u8, "loaf" as *u8, "receipt" as *u8) 45 pr_set_price(PG_STORE, "ralphs_la" as *u8, "milk" as *u8, 360, "2025-06-20" as *u8, "gal" as *u8, "receipt" as *u8) 46 47 let items: *i64 = sys_mmap(8 * 8) as *i64 48 items[0] = "milk" as *u8 as i64 49 items[1] = "eggs" as *u8 as i64 50 items[2] = "bread" as *u8 as i64 51 52 var pass: i64 = 0 53 var tot: i64 = 0 54 55 // T1 stores in areas 56 let s_atx: *i64 = sys_mmap(8 * 64) as *i64 57 let s_la: *i64 = sys_mmap(8 * 64) as *i64 58 let n_atx: i64 = pr_area_stores(PG_STORE, "TX-Austin" as *u8, s_atx) 59 let n_la: i64 = pr_area_stores(PG_STORE, "CA-LA" as *u8, s_la) 60 g_p("TX-Austin stores=" as *u8); g_i(n_atx); g_p(" CA-LA stores=" as *u8); g_i(n_la); g_p("\n" as *u8) 61 tot = tot + 1 62 if n_atx == 3 { if n_la == 1 { pass = pass + 1; g_p("PASS T1 stores registered by area (Austin=3, LA=1)\n" as *u8) } else { g_p("FAIL T1 la\n" as *u8) } } else { g_p("FAIL T1 atx=" as *u8); g_i(n_atx); g_p("\n" as *u8) } 63 64 // T2 observation recorded WITH provenance 65 let pmw: i64 = pr_get_price(PG_STORE, "walmart_atx" as *u8, "milk" as *u8) 66 let date: *u8 = sys_mmap(32); pr_obs_field(PG_STORE, "walmart_atx" as *u8, "milk" as *u8, 1, date) 67 let srcf: *u8 = sys_mmap(32); pr_obs_field(PG_STORE, "walmart_atx" as *u8, "milk" as *u8, 3, srcf) 68 g_p("walmart milk=" as *u8); g_i(pmw); g_p("c date=" as *u8); g_p(date); g_p(" source=" as *u8); g_p(srcf); g_p("\n" as *u8) 69 tot = tot + 1 70 var ok2: i64 = 1 71 if pmw != 298 { ok2 = 0 } 72 if fd_streq(date, "2025-06-20" as *u8) != 1 { ok2 = 0 } 73 if fd_streq(srcf, "receipt" as *u8) != 1 { ok2 = 0 } 74 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 price recorded WITH provenance (298c, dated 2025-06-20, source=receipt)\n" as *u8) } else { g_p("FAIL T2\n" as *u8) } 75 76 // T3 cheapest + median for milk in Austin 77 let csid: *u8 = sys_mmap(48) 78 let cheap: i64 = pr_cheapest(PG_STORE, "TX-Austin" as *u8, "milk" as *u8, csid) 79 let med: i64 = pr_median(PG_STORE, "TX-Austin" as *u8, "milk" as *u8) 80 g_p("milk cheapest=" as *u8); g_p(csid); g_p(" @" as *u8); g_i(cheap); g_p("c median=" as *u8); g_i(med); g_p("c\n" as *u8) 81 tot = tot + 1 82 var ok3: i64 = 1 83 if cheap != 298 { ok3 = 0 } 84 if fd_streq(csid, "walmart_atx" as *u8) != 1 { ok3 = 0 } 85 if med != 320 { ok3 = 0 } 86 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 cheapest milk = Walmart @ $2.98, Austin median = $3.20\n" as *u8) } else { g_p("FAIL T3\n" as *u8) } 87 88 // T4 competitiveness index: discount <100, premium >100 89 let iw: i64 = pr_store_index(PG_STORE, "TX-Austin" as *u8, "walmart_atx" as *u8, items, 3) 90 let ih: i64 = pr_store_index(PG_STORE, "TX-Austin" as *u8, "heb_atx" as *u8, items, 3) 91 let iwf: i64 = pr_store_index(PG_STORE, "TX-Austin" as *u8, "wf_atx" as *u8, items, 3) 92 g_p("index: walmart=" as *u8); g_i(iw); g_p(" heb=" as *u8); g_i(ih); g_p(" wholefoods=" as *u8); g_i(iwf); g_p("\n" as *u8) 93 tot = tot + 1 94 var ok4: i64 = 1 95 if iw >= 100 { ok4 = 0 } // discount: below the area median 96 if iwf <= 100 { ok4 = 0 } // premium: above the area median 97 if iw >= iwf { ok4 = 0 } // and clearly ordered 98 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 'how they're doing': Walmart index<100 (cheaper than median), Whole Foods index>100 (premium)\n" as *u8) } else { g_p("FAIL T4\n" as *u8) } 99 100 // T5 basket totals + cheapest basket; area isolation (LA's milk must NOT change Austin's median) 101 let bw: i64 = pr_basket(PG_STORE, "walmart_atx" as *u8, items, 3) 102 let bh: i64 = pr_basket(PG_STORE, "heb_atx" as *u8, items, 3) 103 let bwf: i64 = pr_basket(PG_STORE, "wf_atx" as *u8, items, 3) 104 let med2: i64 = pr_median(PG_STORE, "TX-Austin" as *u8, "milk" as *u8) 105 g_p("basket: walmart=" as *u8); g_i(bw); g_p(" heb=" as *u8); g_i(bh); g_p(" wf=" as *u8); g_i(bwf); g_p(" austin-milk-median(still)=" as *u8); g_i(med2); g_p("\n" as *u8) 106 tot = tot + 1 107 var ok5: i64 = 1 108 if bw != 686 { ok5 = 0 } 109 if bh != 760 { ok5 = 0 } 110 if bwf != 1120 { ok5 = 0 } 111 if bw >= bh { ok5 = 0 } // Walmart cheapest basket 112 if med2 != 320 { ok5 = 0 } // LA's 360 did NOT leak into Austin's median 113 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 basket: Walmart $6.86 cheapest; area isolation holds (Austin median still $3.20)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) } 114 115 // T6 render + ship 116 let page: *u8 = sys_mmap(65536) 117 let np: i64 = pr_render_area(PG_STORE, "TX-Austin" as *u8, items, 3, page) 118 g_p("rendered Austin price page = " as *u8); g_i(np); g_p(" bytes\n" as *u8) 119 tot = tot + 1 120 var ok6: i64 = 1 121 if as_has_thirdparty_js(page, np) != 0 { ok6 = 0 } 122 if as_contains(page, np, "Walmart Supercenter" as *u8) != 1 { ok6 = 0 } 123 if as_contains(page, np, "Whole Foods Market" as *u8) != 1 { ok6 = 0 } 124 if as_contains(page, np, "$6.86" as *u8) != 1 { ok6 = 0 } 125 if as_contains(page, np, "recorded, date-stamped observations" as *u8) != 1 { ok6 = 0 } // honest note 126 // cheapest-first: Walmart's name must appear before Whole Foods' in the sorted table 127 var posw: i64 = 0 - 1 128 var poswf: i64 = 0 - 1 129 var i: i64 = 0 130 while i < np { 131 if posw < 0 { if as_contains_at(page, np, "Walmart Supercenter" as *u8, i) == 1 { posw = i } } 132 if poswf < 0 { if as_contains_at(page, np, "Whole Foods Market" as *u8, i) == 1 { poswf = i } } 133 i = i + 1 134 } 135 if posw < 0 { ok6 = 0 } 136 if poswf < 0 { ok6 = 0 } 137 if posw > poswf { ok6 = 0 } // Walmart (cheapest) listed before Whole Foods 138 sys_mkdir("knowledge/staging" as *u8, 0x1ed) 139 sys_mkdir("knowledge/staging/price" as *u8, 0x1ed) 140 let sfd: i64 = sys_openat_wr(PG_STAGE_FILE, 420); if sfd >= 0 { sys_write(sfd, page, np); sys_close(sfd) } 141 pub_init() 142 sys_mkdir("knowledge/publish/price-stage" as *u8, 0x1ed) 143 sys_mkdir("knowledge/publish/price-live" as *u8, 0x1ed) 144 pub_submit_to("knowledge/publish/price-queue.tsv" as *u8, PG_STAGE_FILE, "austin.html" as *u8, "nishifoodfamily" as *u8, "nishi-price" as *u8, "internal" as *u8) 145 pub_run_full("knowledge/publish/price-queue.tsv" as *u8, "knowledge/publish/price-ledger.tsv" as *u8, "knowledge/publish/price-stage" as *u8, "knowledge/publish/price-live" as *u8, "publish:price" as *u8) 146 if g_exists(PG_LIVE_FILE) != 1 { ok6 = 0 } 147 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 area price page renders sovereign, cheapest-first, honest note, shipped\n" as *u8) } else { g_p("FAIL T6\n" as *u8) } 148 149 g_p("nx_price_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 150 if pass == tot { g_p(" verdict=GREEN (prices across US stores: cheapest + competitiveness index, sovereign, honest observations)\n" as *u8); return 0 } 151 g_p(" verdict=RED\n" as *u8) 152 return 1 153} 154 155// 1 if needle occurs in hay[0..haylen) starting exactly at position `at`. 156func as_contains_at(hay: *u8, haylen: i64, needle: *u8, at: i64) -> i64 { 157 let nl: i64 = as_len(needle) 158 if at + nl > haylen { return 0 } 159 var k: i64 = 0 160 while k < nl { if hay[at + k] != needle[k] { return 0 } k = k + 1 } 161 return 1 162}