code wiki / _hdl_build / nx_meal_cost_gate.nx
nx_meal_cost_gate.nx source
↩ module page · 148 lines · 8100 B
1// nx_meal_cost_gate.nx -- proves the pantry x price bridge on per-run /tmp fixtures (never its own history).
2// Two cells are BITE-PROVEN: each first demonstrates that the failure mode is REAL against this very data,
3// then that the guard blocks it. A guard proven only against data that could not have tripped it is a comment.
4// license_tier: ORIGINAL No hw writes (Rule 26).
5import "nx_syscalls.nx"
6import "nx_meal_cost.nx"
7import "nx_gate_verdict.nx"
8
9const CG_PFX: i64 = 160
10const CG_FLD: i64 = 256
11const CG_MIN_SALE: i64 = 50
12
13func main(argc: i64, argv: *i64) -> i64 {
14 let ctr: *i64 = gv_ctr()
15 gv_head("nx_meal_cost -- what this meal costs given what you already have, and what is genuinely cheap" as *u8)
16
17 let ep: i64 = sys_now_realtime_sec()
18 let inv: *u8 = sys_mmap(CG_PFX)
19 var io: i64 = as_append(inv, 0, "/tmp/mcinv_" as *u8)
20 io = fd_apnum(inv, io, ep)
21 io = as_append(inv, io, "-" as *u8)
22 inv[io] = 0 as u8
23 let pr: *u8 = sys_mmap(CG_PFX)
24 var ro: i64 = as_append(pr, 0, "/tmp/mcpr_" as *u8)
25 ro = fd_apnum(pr, ro, ep)
26 ro = as_append(pr, ro, "-" as *u8)
27 pr[ro] = 0 as u8
28
29 let dom: *u8 = "family" as *u8
30 let area: *u8 = "testarea" as *u8
31
32 // pantry: the unit already holds garlic (3 bulbs, 90c of stock value)
33 iv_add(inv, dom, "garlic" as *u8, "Garlic" as *u8, "produce" as *u8, 3, "bulb" as *u8, "pantry" as *u8, 90, 0, "" as *u8)
34
35 // three stores in the area. `onlybeef` stocks ONE ingredient at an absurd price -- the coverage trap.
36 pr_add_store(pr, "bigmart" as *u8, "BigMart" as *u8, "BigMart" as *u8, area, "WA" as *u8, "weekly circular" as *u8)
37 pr_add_store(pr, "corner" as *u8, "Corner Store" as *u8, "indie" as *u8, area, "WA" as *u8, "shelf tag" as *u8)
38 pr_add_store(pr, "onlybeef" as *u8, "Only Beef" as *u8, "indie" as *u8, area, "WA" as *u8, "shelf tag" as *u8)
39
40 pr_set_price(pr, "bigmart" as *u8, "beef" as *u8, 899, "2026-08-01" as *u8, "lb" as *u8, "weekly circular" as *u8)
41 pr_set_price(pr, "bigmart" as *u8, "broccoli" as *u8, 249, "2026-08-01" as *u8, "lb" as *u8, "weekly circular" as *u8)
42 pr_set_price(pr, "corner" as *u8, "beef" as *u8, 1099, "2026-08-01" as *u8, "lb" as *u8, "shelf tag" as *u8)
43 pr_set_price(pr, "corner" as *u8, "broccoli" as *u8, 199, "2026-08-01" as *u8, "lb" as *u8, "shelf tag" as *u8)
44 pr_set_price(pr, "onlybeef" as *u8, "beef" as *u8, 100, "2026-08-02" as *u8, "lb" as *u8, "clearance tag" as *u8)
45
46 // the recipe: beef + broccoli + garlic + truffle. Truffle is deliberately UNPRICED.
47 let items: *i64 = sys_mmap(8 * 16) as *i64
48 items[0] = "beef" as *u8 as i64
49 items[1] = "broccoli" as *u8 as i64
50 items[2] = "garlic" as *u8 as i64
51 items[3] = "truffle" as *u8 as i64
52 // the same recipe minus the unpriced ingredient
53 let items3: *i64 = sys_mmap(8 * 16) as *i64
54 items3[0] = "beef" as *u8 as i64
55 items3[1] = "broccoli" as *u8 as i64
56 items3[2] = "garlic" as *u8 as i64
57 // just the two we must buy (for the store-coverage question)
58 let buy2: *i64 = sys_mmap(8 * 16) as *i64
59 buy2[0] = "beef" as *u8 as i64
60 buy2[1] = "broccoli" as *u8 as i64
61
62 // ---- T1 classification ----
63 var t1: i64 = 1
64 if mc_status(inv, dom, pr, area, "garlic" as *u8) != MC_HAVE { t1 = 0 }
65 if mc_status(inv, dom, pr, area, "beef" as *u8) != MC_BUY { t1 = 0 }
66 if mc_status(inv, dom, pr, area, "truffle" as *u8) != MC_UNPRICED { t1 = 0 }
67 gv_check("T1 each ingredient classifies HAVE / BUY / UNPRICED against the real pantry" as *u8, t1, ctr)
68
69 // ---- T2 the meal summary ----
70 let s4: *i64 = sys_mmap(8 * MC_SLOTS) as *i64
71 let todo: i64 = mc_meal(inv, dom, pr, area, items, 4, CG_MIN_SALE, s4)
72 var t2: i64 = 1
73 if s4[MC_N_HAVE] != 1 { t2 = 0 }
74 if s4[MC_N_BUY] != 2 { t2 = 0 }
75 if s4[MC_N_UNPRICED] != 1 { t2 = 0 }
76 if s4[MC_BUY_CENTS] != 299 { t2 = 0 }
77 if s4[MC_PANTRY_CENTS] != 90 { t2 = 0 }
78 if todo != 3 { t2 = 0 }
79 gv_check("T2 meal totals: 1 on hand, 2 to buy at 299c, 1 unpriced, 90c drawn from stock" as *u8, t2, ctr)
80
81 // ---- T3 BITE the unpriced guard. BOTH meals total 299c -- the NUMBER cannot tell them apart, which is
82 // exactly why completeness has to be reported separately from the total.
83 let s3: *i64 = sys_mmap(8 * MC_SLOTS) as *i64
84 mc_meal(inv, dom, pr, area, items3, 3, CG_MIN_SALE, s3)
85 var unp_bad: i64 = 0
86 if mc_total_is_complete(s4) == 0 { unp_bad = 1 }
87 var unp_good: i64 = 0
88 if mc_total_is_complete(s3) == 0 { unp_good = 1 }
89 var same_total: i64 = 0
90 if s3[MC_BUY_CENTS] == s4[MC_BUY_CENTS] { same_total = 1 }
91 gv_check("T3a the priced and part-priced meals post the IDENTICAL 299c total (the number cannot warn you)" as *u8, same_total, ctr)
92 gv_bite("T3b completeness flag fires on the meal hiding an unpriced ingredient, silent on the fully priced one" as *u8, unp_bad, unp_good, ctr)
93
94 // ---- T4 sale detection is MEASURED and fails closed ----
95 var t4: i64 = 1
96 if mc_sale_delta(pr, area, "beef" as *u8) != 799 { t4 = 0 } // median 899 - cheapest 100
97 if mc_sale_delta(pr, area, "broccoli" as *u8) != 50 { t4 = 0 } // median 249 - cheapest 199
98 if mc_is_on_sale(pr, area, "beef" as *u8, CG_MIN_SALE) != 1 { t4 = 0 }
99 if mc_is_on_sale(pr, area, "beef" as *u8, 100000) != 0 { t4 = 0 } // threshold above the delta
100 if mc_is_on_sale(pr, area, "beef" as *u8, 0) != 0 { t4 = 0 } // FAIL-CLOSED on an unset threshold
101 if mc_is_on_sale(pr, area, "truffle" as *u8, CG_MIN_SALE) != 0 { t4 = 0 } // unknown is not a bargain
102 if s4[MC_SALE_CENTS] != 849 { t4 = 0 }
103 gv_check("T4 on-sale is measured vs the area median, unset threshold marks NOTHING, unknown is not cheap" as *u8, t4, ctr)
104
105 // ---- T5 BITE the store-coverage guard ----
106 let sid: *u8 = sys_mmap(CG_FLD)
107 let best: i64 = mc_best_store(pr, area, buy2, 2, sid)
108 let naive: i64 = pr_basket(pr, "onlybeef" as *u8, buy2, 2)
109 var cov_bad: i64 = 0
110 if naive < best { cov_bad = 1 } // the trap is REAL against this very data
111 var cov_good: i64 = 0
112 if fd_streq(sid, "onlybeef" as *u8) == 1 { cov_good = 1 } // the guard must not have picked it
113 var t5: i64 = 1
114 if best != 1148 { t5 = 0 } // BigMart 899 + 249
115 if fd_streq(sid, "bigmart" as *u8) == 0 { t5 = 0 }
116 if mc_store_covers(pr, "onlybeef" as *u8, buy2, 2) != 1 { t5 = 0 }
117 gv_bite("T5a the naive basket ranks the 1-of-2 store cheapest, coverage guard refuses to pick it" as *u8, cov_bad, cov_good, ctr)
118 gv_check("T5b cheapest FULLY-STOCKING store is BigMart at 1148c, not the 100c one-item shop" as *u8, t5, ctr)
119
120 // ---- T6 shopping list carries unknown as -1, never as free ----
121 let bi: *i64 = sys_mmap(8 * 16) as *i64
122 let bc: *i64 = sys_mmap(8 * 16) as *i64
123 let nb: i64 = mc_buy_list(inv, dom, pr, area, items, 4, bi, bc)
124 var t6: i64 = 1
125 if nb != 3 { t6 = 0 }
126 if bc[0] != 100 { t6 = 0 }
127 if bc[1] != 199 { t6 = 0 }
128 if bc[2] != (0 - 1) { t6 = 0 }
129 if fd_streq(bi[2] as *u8, "truffle" as *u8) == 0 { t6 = 0 }
130 let hi: *i64 = sys_mmap(8 * 16) as *i64
131 if mc_have_list(inv, dom, items, 4, hi) != 1 { t6 = 0 }
132 if fd_streq(hi[0] as *u8, "garlic" as *u8) == 0 { t6 = 0 }
133 gv_check("T6 shopping list marks the unpriced item -1 (never 0) and the on-hand list holds only garlic" as *u8, t6, ctr)
134
135 // ---- T7 a price carries its provenance ----
136 let f: *u8 = sys_mmap(CG_FLD)
137 var t7: i64 = 1
138 if mc_price_asof(pr, area, "beef" as *u8, f) == 0 { t7 = 0 }
139 if fd_streq(f, "2026-08-02" as *u8) == 0 { t7 = 0 }
140 mc_price_source(pr, area, "beef" as *u8, f)
141 if fd_streq(f, "clearance tag" as *u8) == 0 { t7 = 0 }
142 if mc_price_asof(pr, area, "truffle" as *u8, f) != 0 { t7 = 0 }
143 gv_check("T7 the winning price carries its observation date + source (no implied live pricing)" as *u8, t7, ctr)
144
145 let rc: i64 = gv_verdict("MEAL-COST" as *u8, ctr, "unpriced is never free; a store must stock the basket to win it; sale is measured vs median" as *u8)
146 sys_exit(rc)
147 return 0
148}