code wiki / _hdl_build / nx_sourcing_path.nx

nx_sourcing_path.nx source

↩ module page · 382 lines · 18018 B

1// nx_sourcing_path.nx -- SOVEREIGN SOURCING-MIX + RESHORE-PATH PRICER. 2// 3// Operator 2026-08-06: "i'd like to be market competitive and shift to full american supply chain 4// with local government help." That is a PATH, not a point. nx_landed_cost prices ONE shipment from 5// ONE corridor; this organ prices a whole BILL OF MATERIALS whose components come from DIFFERENT 6// corridors, and then prices the CURVE from today's mix to fully domestic -- with the incentive 7// layer that is the actual bridge. 8// 9// COMPOSES, NEVER DUPLICATES (rule 15). It imports nx_landed_cost.nx for the PROVEN duty core 10// (lc_is_domestic / lc_pref_ok / lc_s301_applies / lc_is_col2 + the corridor plane readers, all 11// gate-proven 15/15 on 2026-08-06) and nx_supplychain_case.nx for domestic-content and weighted 12// risk. A NishiLang target CAN import another target -- measured, not assumed (nx_lcimport_probe: 13// the importing main() wins and every imported symbol resolves). So there is ONE duty 14// implementation in the estate, not two. 15// 16// ★THE CORRECTNESS POINT THAT FORCED THIS DESIGN: **MPF AND HMF ARE PER CUSTOMS ENTRY, NOT PER LINE 17// ITEM.** Pricing a BOM by calling a per-shipment pricer once per component charges the $33.58 MPF 18// FLOOR once per component. On a 3-part BOM of $1,000 parts that is 10,449c of fees instead of 19// 3,733c -- a ~2.8x over-count that lands hardest on small runs, i.e. exactly the domestic pilot the 20// operator wants to justify. Entry fees are therefore computed ONCE on the AGGREGATE dutiable value. 21// 22// INTEGER-EXACT, NO FLOAT (sovereign law): money in CENTS, rates in BASIS POINTS, shares in PERMIL. 23// 24// DATA, NOT CODE (rule 11): the BOM is a seg-store plane; so are the incentives. Adding a component 25// or a grant programme is a plane row, no reship. 26// BOM row `srcbom-` : <id> TAB <label> TAB <cc> TAB <value_c> TAB <col1bps> TAB <col2bps> 27// TAB <s301bps> TAB <single01> TAB <energyrisk0-100> TAB <us_value_c> 28// INCENTIVE `incentive-` : <id> TAB <label> TAB <permil_of_domestic_value> TAB <note> 29// us_value_c = what THIS component would cost made domestically. It is the field that makes the 30// reshore path priceable at all; without it "shift to domestic" has no cost. 31// 32// VERBS: 33// nx_sourcing_path mix <bomprefix> <units> <unitprice_c> <ocean01> <pref01> [incprefix] 34// nx_sourcing_path path <bomprefix> <units> <unitprice_c> <ocean01> <pref01> [incprefix] 35// nx_sourcing_path selftest 36// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 37import "nx_syscalls.nx" 38import "nx_landed_cost.nx" 39import "nx_supplychain_case.nx" 40 41const SP_MAXROWS: i64 = 64 42const SP_BOMBUF: i64 = 65536 43const SP_OUTBUF: i64 = 65536 44const SP_FLDBUF: i64 = 256 45const SP_PERMIL: i64 = 1000 46const SP_ORIGIN_DOM: i64 = 0 47const SP_ORIGIN_ALLIED: i64 = 1 48const SP_ORIGIN_ADVERSARY: i64 = 2 49// selftest fixtures -- every value hand-computed in the comment beside its assertion 50const SP_T_1M: i64 = 1000000 51const SP_T_100K: i64 = 100000 52const SP_T_300K: i64 = 300000 53const SP_T_100M: i64 = 100000000 54const SP_T_MPF_1M: i64 = 3464 55const SP_T_HMF_1M: i64 = 1250 56const SP_T_FEES_1M: i64 = 4714 57const SP_T_FEES_100K: i64 = 3483 58const SP_T_FEES_300K: i64 = 3733 59const SP_T_FEES_3X100K: i64 = 10449 60 61func spw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 62 63// ---- CORE 1: ENTRY FEES. The whole reason this organ exists. Computed ONCE on the aggregate 64// dutiable value of the entry, never per line item. A fully domestic mix has NO customs entry at 65// all, so its fees are structurally zero -- not "a small number". 66func sp_entry_fees(dutiable_c: i64, ocean: i64, k: *i64) -> i64 { 67 if dutiable_c <= 0 { k[0] = 0; k[1] = 0; return 0 } 68 var mpf: i64 = dutiable_c * LC_MPF_PPM / LC_MAGIC_1000000 69 if mpf < LC_MPF_MIN_C { mpf = LC_MPF_MIN_C } 70 if mpf > LC_MPF_MAX_C { mpf = LC_MPF_MAX_C } 71 var hmf: i64 = 0 72 if ocean == 1 { hmf = dutiable_c * LC_HMF_PPM / LC_MAGIC_1000000 } 73 k[0] = mpf 74 k[1] = hmf 75 return mpf + hmf 76} 77 78// ---- CORE 2: THE INCENTIVE LAYER. Local/state/federal support is the bridge that makes the 79// domestic end competitive. It scales with DOMESTIC value, so it strengthens as the mix reshores -- 80// that is what bends the path curve. Leaving it unmodelled is itself a bias: it prices the cost of 81// reshoring and none of the help. 82func sp_incentive_c(domestic_value_c: i64, permil: i64) -> i64 { 83 if domestic_value_c <= 0 { return 0 } 84 if permil <= 0 { return 0 } 85 return domestic_value_c * permil / SP_PERMIL 86} 87 88// ---- origin class for the risk model, DERIVED from the corridor plane (never a second table). 89func sp_origin_class(rbuf: *u8, rn: i64, cc: *u8) -> i64 { 90 if lc_is_domestic(rbuf, rn, cc) == 1 { return SP_ORIGIN_DOM } 91 if lc_is_col2(rbuf, rn, cc) == 1 { return SP_ORIGIN_ADVERSARY } 92 if lc_s301_applies(rbuf, rn, cc) == 1 { return SP_ORIGIN_ADVERSARY } 93 return SP_ORIGIN_ALLIED 94} 95 96// ---- plane row iteration (line/TAB only, same discipline as the corridor reader) 97func sp_row_start(buf: *u8, n: i64, want: i64) -> i64 { 98 var i: i64 = 0 99 var r: i64 = 0 100 while i < n { 101 if r == want { return i } 102 var adv: i64 = 1 103 while adv == 1 { 104 if i >= n { adv = 0 } else { 105 if buf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 106 } 107 } 108 r = r + 1 109 } 110 return 0 - 1 111} 112func sp_rowcount(buf: *u8, n: i64) -> i64 { 113 var i: i64 = 0 114 var r: i64 = 0 115 while i < n { 116 var any: i64 = 0 117 var adv: i64 = 1 118 while adv == 1 { 119 if i >= n { adv = 0 } else { 120 if buf[i] == (10 as u8) { i = i + 1; adv = 0 } else { any = 1; i = i + 1 } 121 } 122 } 123 if any == 1 { r = r + 1 } 124 } 125 return r 126} 127func sp_num(buf: *u8, n: i64, rs: i64, idx: i64, scratch: *u8) -> i64 { 128 lc_extract(buf, n, rs, idx, scratch, SP_FLDBUF) 129 return lc_atoi(scratch) 130} 131 132func sp_selftest() -> i64 { 133 let k: *i64 = sys_mmap(64) as *i64 134 var pass: i64 = 0 135 var total: i64 = 0 136 // T1-T3 reference entry: $10,000 dutiable, ocean. mpf = 0.3464% = 3464c (between floor and cap); 137 // hmf = 0.125% = 1250c; total 4714c. 138 let f1: i64 = sp_entry_fees(SP_T_1M, 1, k) 139 total = total + 1; if f1 == SP_T_FEES_1M { pass = pass + 1 } 140 total = total + 1; if k[0] == SP_T_MPF_1M { pass = pass + 1 } 141 total = total + 1; if k[1] == SP_T_HMF_1M { pass = pass + 1 } 142 // T4/T5 FLOOR ENGAGED: $1,000 dutiable -> raw mpf 346c is BELOW the 3358c statutory floor, so the 143 // floor applies; + 125c hmf = 3483c. 144 let f2: i64 = sp_entry_fees(SP_T_100K, 1, k) 145 total = total + 1; if f2 == SP_T_FEES_100K { pass = pass + 1 } 146 total = total + 1; if k[0] == LC_MPF_MIN_C { pass = pass + 1 } 147 // T6/T7 ★THE PER-ENTRY TOOTH. Three $1,000 components in ONE entry: aggregate dutiable $3,000 -> 148 // raw mpf 1039c, still under the floor -> 3358c + 375c hmf = 3733c. Pricing them as three 149 // SEPARATE shipments would charge the floor THREE times = 10449c. The aggregate MUST be strictly 150 // less, and by nearly 3x. This is the defect that a per-component pricer would have shipped. 151 let f3: i64 = sp_entry_fees(SP_T_300K, 1, k) 152 total = total + 1; if f3 == SP_T_FEES_300K { pass = pass + 1 } 153 total = total + 1; if f3 < SP_T_FEES_3X100K { pass = pass + 1 } 154 // T8/T9 FULLY DOMESTIC = NO CUSTOMS ENTRY EXISTS. Not a small fee -- a structural zero. 155 let f4: i64 = sp_entry_fees(0, 1, k) 156 total = total + 1; if f4 == 0 { pass = pass + 1 } 157 total = total + 1; if k[0] == 0 { pass = pass + 1 } 158 // T10 HMF IS OCEAN-GATED: same entry, air freight -> mpf only. 159 let f5: i64 = sp_entry_fees(SP_T_1M, 0, k) 160 total = total + 1; if f5 == SP_T_MPF_1M { pass = pass + 1 } 161 // T11 CAP ENGAGED at the other end: $1,000,000 dutiable -> raw mpf 346400c, clamped to 65150c. 162 sp_entry_fees(SP_T_100M, 0, k) 163 total = total + 1; if k[0] == LC_MPF_MAX_C { pass = pass + 1 } 164 // T12-T14 INCENTIVE LAYER, both directions: help scales with DOMESTIC value; no domestic value or 165 // no programme means no help (an incentive can never be conjured from an import). 166 total = total + 1; if sp_incentive_c(SP_T_1M, SP_PERMIL / 10) == SP_T_100K { pass = pass + 1 } 167 total = total + 1; if sp_incentive_c(0, SP_PERMIL / 10) == 0 { pass = pass + 1 } 168 total = total + 1; if sp_incentive_c(SP_T_1M, 0) == 0 { pass = pass + 1 } 169 // T15-T18 ORIGIN CLASS derived from the corridor plane -- one table, never a second. 170 let rbuf: *u8 = sys_mmap(LC_REGBUF) 171 let rn: i64 = lc_load_regime(rbuf) 172 total = total + 1; if sp_origin_class(rbuf, rn, "us" as *u8) == SP_ORIGIN_DOM { pass = pass + 1 } 173 total = total + 1; if sp_origin_class(rbuf, rn, "cn" as *u8) == SP_ORIGIN_ADVERSARY { pass = pass + 1 } 174 total = total + 1; if sp_origin_class(rbuf, rn, "ru" as *u8) == SP_ORIGIN_ADVERSARY { pass = pass + 1 } 175 total = total + 1; if sp_origin_class(rbuf, rn, "mx" as *u8) == SP_ORIGIN_ALLIED { pass = pass + 1 } 176 let line: *u8 = sys_mmap(512) 177 var o: i64 = 0 178 o = lc_lit(line, o, "SELFTEST " as *u8) 179 o = lc_num(line, o, pass) 180 o = lc_lit(line, o, "/" as *u8) 181 o = lc_num(line, o, total) 182 if pass == total { o = lc_lit(line, o, " VERDICT=GREEN (entry fees aggregate PER ENTRY not per line -- floor and cap both engaged; domestic = structural zero; incentive scales with domestic value; origin class derived from the corridor plane)\n" as *u8) } else { o = lc_lit(line, o, " VERDICT=RED\n" as *u8) } 183 line[o] = 0 as u8 184 spw(1, line) 185 if pass == total { return 0 } 186 return 1 187} 188 189// total incentive strength in permil of domestic value = SUM of every programme row in the plane. 190// Data-driven: adding a state grant or a federal credit is a plane row, not a code change. 191func sp_inc_permil(buf: *u8, n: i64, scratch: *u8) -> i64 { 192 var t: i64 = 0 193 var r: i64 = 0 194 let cnt: i64 = sp_rowcount(buf, n) 195 while r < cnt { 196 let rs: i64 = sp_row_start(buf, n, r) 197 if rs >= 0 { t = t + sp_num(buf, n, rs, 2, scratch) } 198 r = r + 1 199 } 200 return t 201} 202 203// ---- THE MIX PRICER. `domesticate` = how many components (cheapest-to-reshore FIRST) are forced 204// domestic; 0 = today's mix, cnt = fully American. `path` just walks this from 0 to cnt, which is 205// why the curve costs one implementation rather than two. 206// res: 0 goods 1 duty 2 mpf 3 hmf 4 landed 5 dom_permil 6 risk 7 incentive 8 net_landed 9 margin_permil 10 n 207func sp_mix(bom: *u8, bn: i64, rbuf: *u8, rn: i64, units: i64, price: i64, ocean: i64, pref: i64, incpermil: i64, domesticate: i64, res: *i64) -> i64 { 208 let scratch: *u8 = sys_mmap(SP_FLDBUF) 209 var cnt: i64 = sp_rowcount(bom, bn) 210 if cnt > SP_MAXROWS { cnt = SP_MAXROWS } 211 let value: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 212 let usval: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 213 let col1a: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 214 let col2a: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 215 let s301a: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 216 let single: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 217 let erisk: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 218 let origin: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 219 let idx: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 220 let ccs: *u8 = sys_mmap(SP_MAXROWS * 8) 221 var i: i64 = 0 222 while i < cnt { 223 let rs: i64 = sp_row_start(bom, bn, i) 224 let slot: *u8 = (ccs as i64 + i * 8) as *u8 225 lc_extract(bom, bn, rs, 2, slot, 8) 226 value[i] = sp_num(bom, bn, rs, 3, scratch) 227 col1a[i] = sp_num(bom, bn, rs, 4, scratch) 228 col2a[i] = sp_num(bom, bn, rs, 5, scratch) 229 s301a[i] = sp_num(bom, bn, rs, 6, scratch) 230 single[i] = sp_num(bom, bn, rs, 7, scratch) 231 erisk[i] = sp_num(bom, bn, rs, 8, scratch) 232 usval[i] = sp_num(bom, bn, rs, 9, scratch) 233 idx[i] = i 234 i = i + 1 235 } 236 // order by RESHORE DELTA ascending -- domesticate the cheapest-to-move component first. That 237 // ordering is the whole reason the path is a usable plan and not just an endpoint comparison. 238 var a: i64 = 0 239 while a < cnt { 240 var best: i64 = a 241 var b: i64 = a + 1 242 while b < cnt { 243 let db: i64 = usval[idx[b]] - value[idx[b]] 244 let dk: i64 = usval[idx[best]] - value[idx[best]] 245 if db < dk { best = b } 246 b = b + 1 247 } 248 let t: i64 = idx[a]; idx[a] = idx[best]; idx[best] = t 249 a = a + 1 250 } 251 // force the first `domesticate` of that order to domestic supply, at their domestic cost 252 var forced: *i64 = sys_mmap(SP_MAXROWS * 8) as *i64 253 i = 0 254 while i < cnt { forced[i] = 0; i = i + 1 } 255 i = 0 256 while i < domesticate { if i < cnt { forced[idx[i]] = 1 } i = i + 1 } 257 258 var goods: i64 = 0 259 var duty: i64 = 0 260 var dutiable: i64 = 0 261 var domvalue: i64 = 0 262 i = 0 263 while i < cnt { 264 let cc: *u8 = (ccs as i64 + i * 8) as *u8 265 var v: i64 = value[i] 266 var isdom: i64 = lc_is_domestic(rbuf, rn, cc) 267 if forced[i] == 1 { isdom = 1; v = usval[i] } 268 if isdom == 1 { 269 origin[i] = SP_ORIGIN_DOM 270 domvalue = domvalue + v 271 } else { 272 origin[i] = sp_origin_class(rbuf, rn, cc) 273 let col2f: i64 = lc_is_col2(rbuf, rn, cc) 274 var rate: i64 = col1a[i] 275 if col2f == 1 { rate = col2a[i] } 276 if pref == 1 { if lc_pref_ok(rbuf, rn, cc, col2f) == 1 { rate = 0 } } 277 var d: i64 = v * rate / LC_MAGIC_10000 278 if lc_s301_applies(rbuf, rn, cc) == 1 { d = d + v * s301a[i] / LC_MAGIC_10000 } 279 duty = duty + d 280 dutiable = dutiable + v 281 } 282 value[i] = v 283 goods = goods + v 284 i = i + 1 285 } 286 let fk: *i64 = sys_mmap(64) as *i64 287 let fees: i64 = sp_entry_fees(dutiable, ocean, fk) 288 let landed: i64 = goods + duty + fees 289 let inc: i64 = sp_incentive_c(domvalue, incpermil) 290 let net: i64 = landed - inc 291 let revenue: i64 = price * units 292 var mperm: i64 = 0 293 if revenue > 0 { mperm = (revenue - net) * SP_PERMIL / revenue } 294 res[0] = goods; res[1] = duty; res[2] = fk[0]; res[3] = fk[1]; res[4] = landed 295 res[5] = sc_domestic_permil(origin, value, cnt) 296 res[6] = sc_weighted_risk(origin, erisk, single, value, cnt) 297 res[7] = inc; res[8] = net; res[9] = mperm; res[10] = cnt 298 return cnt 299} 300 301func sp_emit(out: *u8, o: i64, res: *i64, step: i64) -> i64 { 302 var p: i64 = o 303 p = lc_lit(out, p, "{\"domesticated\":" as *u8) 304 p = lc_num(out, p, step) 305 p = lc_lit(out, p, ",\"components\":" as *u8) 306 p = lc_num(out, p, res[10]) 307 p = lc_lit(out, p, ",\"goods_c\":" as *u8) 308 p = lc_num(out, p, res[0]) 309 p = lc_lit(out, p, ",\"duty_c\":" as *u8) 310 p = lc_num(out, p, res[1]) 311 p = lc_lit(out, p, ",\"mpf_c\":" as *u8) 312 p = lc_num(out, p, res[2]) 313 p = lc_lit(out, p, ",\"hmf_c\":" as *u8) 314 p = lc_num(out, p, res[3]) 315 p = lc_lit(out, p, ",\"landed_c\":" as *u8) 316 p = lc_num(out, p, res[4]) 317 p = lc_lit(out, p, ",\"domestic_permil\":" as *u8) 318 p = lc_num(out, p, res[5]) 319 p = lc_lit(out, p, ",\"supply_risk\":" as *u8) 320 p = lc_num(out, p, res[6]) 321 p = lc_lit(out, p, ",\"incentive_c\":" as *u8) 322 p = lc_num(out, p, res[7]) 323 p = lc_lit(out, p, ",\"net_landed_c\":" as *u8) 324 p = lc_num(out, p, res[8]) 325 p = lc_lit(out, p, ",\"margin_permil\":" as *u8) 326 p = lc_num(out, p, res[9]) 327 p = lc_lit(out, p, "}" as *u8) 328 return p 329} 330 331func main(argc: i64, argv: *i64) -> i64 { 332 if argc < 2 { spw(2, "usage: nx_sourcing_path {mix|path} <bomprefix> <units> <unitprice_c> <ocean01> <pref01> [incprefix] | selftest\n" as *u8); return 2 } 333 let verb: *u8 = argv[1] as *u8 334 if lc_eq(verb, "selftest" as *u8) == 1 { return sp_selftest() } 335 let ismix: i64 = lc_eq(verb, "mix" as *u8) 336 let ispath: i64 = lc_eq(verb, "path" as *u8) 337 if ismix == 0 { if ispath == 0 { spw(2, "usage: nx_sourcing_path {mix|path} <bomprefix> <units> <unitprice_c> <ocean01> <pref01> [incprefix] | selftest\n" as *u8); return 2 } } 338 if argc < 7 { spw(2, "needs 5 args: <bomprefix> <units> <unitprice_c> <ocean01> <pref01> [incprefix]\n" as *u8); return 2 } 339 let bom: *u8 = sys_mmap(SP_BOMBUF) 340 let bn: i64 = sts_load(argv[2] as *u8, bom, SP_BOMBUF) 341 if bn <= 0 { spw(2, "BOM PLANE EMPTY OR ABSENT -- refusing to price a mix that was never described (no fabricated components)\n" as *u8); return 3 } 342 let units: i64 = lc_atoi(argv[3] as *u8) 343 let price: i64 = lc_atoi(argv[4] as *u8) 344 let ocean: i64 = lc_atoi(argv[5] as *u8) 345 let pref: i64 = lc_atoi(argv[6] as *u8) 346 var incpermil: i64 = 0 347 if argc >= 8 { 348 let ibuf: *u8 = sys_mmap(SP_BOMBUF) 349 let inn: i64 = sts_load(argv[7] as *u8, ibuf, SP_BOMBUF) 350 if inn > 0 { incpermil = sp_inc_permil(ibuf, inn, sys_mmap(SP_FLDBUF)) } 351 } 352 let rbuf: *u8 = sys_mmap(LC_REGBUF) 353 let rn: i64 = lc_load_regime(rbuf) 354 let res: *i64 = sys_mmap(128) as *i64 355 let out: *u8 = sys_mmap(SP_OUTBUF) 356 var o: i64 = 0 357 if ismix == 1 { 358 sp_mix(bom, bn, rbuf, rn, units, price, ocean, pref, incpermil, 0, res) 359 o = sp_emit(out, o, res, 0) 360 out[o] = 10 as u8; o = o + 1 361 out[o] = 0 as u8 362 spw(1, out) 363 return 0 364 } 365 // path: walk 0 -> every component domestic. The curve, not the endpoints. 366 let n0: i64 = sp_mix(bom, bn, rbuf, rn, units, price, ocean, pref, incpermil, 0, res) 367 o = lc_lit(out, o, "{\"incentive_permil_of_domestic\":" as *u8) 368 o = lc_num(out, o, incpermil) 369 o = lc_lit(out, o, ",\"steps\":[" as *u8) 370 var s: i64 = 0 371 while s <= n0 { 372 if s > 0 { out[o] = 44 as u8; o = o + 1 } 373 sp_mix(bom, bn, rbuf, rn, units, price, ocean, pref, incpermil, s, res) 374 o = sp_emit(out, o, res, s) 375 s = s + 1 376 } 377 o = lc_lit(out, o, "]}" as *u8) 378 out[o] = 10 as u8; o = o + 1 379 out[o] = 0 as u8 380 spw(1, out) 381 return 0 382}