code wiki / _hdl_build / nx_landed_cost.nx

nx_landed_cost.nx source

↩ module page · 572 lines · 30264 B

1// nx_landed_cost.nx -- SOVEREIGN LANDED-COST ENGINE for the import/export corridor program. 2// 3// The API/MCP/agentic tier of the /trade.html calculator (operator: "an emitting calculator for 4// these import exports ... we want to do it with russia and lots of other countries ... it needs 5// to scale"). The HTML page prices ONE shipment for a human; THIS organ prices ANY shipment for 6// an AGENT or WORKFLOW -- one tools/call returns the full 2026 US duty stack as structured JSON, 7// so a workflow can fan out over a whole supplier catalog x many corridors and rank by margin. 8// 9// INTEGER-EXACT, NO FLOAT (sovereign law): money in CENTS, duty rates in BASIS POINTS (1% = 100bps), 10// fees in PPM, margins in PERMIL. Every figure is a pure integer transform of the inputs -- no 11// rounding drift, reproducible, gate-checkable against hand arithmetic. 12// 13// REGIME LAYER (the researched, stable part -- 2026-07, verify before a real transaction): 14// * PNTR/NTR status picks Column 1 vs Column 2. Belarus + Russia = PNTR SUSPENDED = Column 2 15// (punitive) + they are the only col2 corridors here. All others = Column 1. 16// * EXIM Country Limitation Schedule: CLOSED for Belarus + Russia; open elsewhere. 17// * Section 122 (the 10% global surcharge) EXPIRED 2026-07-24 by operation of law -> default OFF; 18// pass s122=1 only to model a replacement surcharge. Section 232/301 are product-specific inputs. 19// The PRODUCT duty rates (col1/col2/232/301 bps) are INPUTS -- they are HTS-line-specific and must 20// come from a licensed broker; the organ supplies the regime logic + fee math around them. 21// 22// VERBS: 23// nx_landed_cost calc <cc> <fob_c> <freight_c> <ins_c> <units> <col1bps> <col2bps> <s232bps> 24// <s301bps> <unitprice_c> <ocean01> <s122_01> <pref01> <other_c> -> JSON 25// nx_landed_cost selftest -> known-value gate (Belarus + Central-Asia reference shipments) -> VERDICT 26// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 27import "nx_syscalls.nx" 28import "nx_seg_store.nx" 29import "nx_store_seed_lib.nx" 30const LC_MAGIC_10000: i64 = 10000 31const LC_MAGIC_1000000: i64 = 1000000 32const LC_MAGIC_4096: i64 = 4096 33const LC_MAGIC_4000000: i64 = 4000000 34const LC_MAGIC_380000: i64 = 380000 35const LC_MAGIC_42000: i64 = 42000 36const LC_MAGIC_2000: i64 = 2000 37const LC_MAGIC_3500: i64 = 3500 38const LC_MAGIC_3800: i64 = 3800 39const LC_MAGIC_183500: i64 = 183500 40const LC_MAGIC_1400000: i64 = 1400000 41const LC_MAGIC_13856: i64 = 13856 42const LC_MAGIC_3464: i64 = 3464 43const LC_MAGIC_5000: i64 = 5000 44const LC_MAGIC_1418856: i64 = 1418856 45const LC_MAGIC_6024356: i64 = 6024356 46const LC_MAGIC_148000: i64 = 148000 47const LC_MAGIC_166856: i64 = 166856 48 49const LC_MPF_PPM: i64 = 3464 // 0.3464% 50const LC_MPF_MIN_C: i64 = 3358 // $33.58 51const LC_MPF_MAX_C: i64 = 65150 // $651.50 52const LC_HMF_PPM: i64 = 1250 // 0.125% (ocean only) 53const LC_S122_BPS: i64 = 1000 // 10% 54const LC_CARDBUF: i64 = 2048 // corridor-card JSON buffer 55const LC_REGBUF: i64 = 65536 // seg-store regime plane load buffer 56const LC_NOTEBUF: i64 = 2048 // corridor note field buffer 57const LC_MAXC: i64 = 64 // max corridors ranked in one call 58 59func lw(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 } 60func lc_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 61 62func lc_eq(a: *u8, b: *u8) -> i64 { 63 var i: i64 = 0 64 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 65 if b[i] != (0 as u8) { return 0 } 66 return 1 67} 68 69// parse a non-negative decimal (fail-soft: non-digits stop the scan; empty -> 0) 70func lc_atoi(s: *u8) -> i64 { 71 var v: i64 = 0 72 var i: i64 = 0 73 if s[0] == (45 as u8) { i = 1 } // tolerate a leading '-', treat magnitude 74 while s[i] != (0 as u8) { 75 let c: i64 = s[i] as i64 76 if c < 48 { return v } 77 if c > 57 { return v } 78 v = v * 10 + (c - 48) 79 i = i + 1 80 } 81 return v 82} 83 84func lc_lit(out: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { out[o + i] = s[i]; i = i + 1 } return o + i } 85func lc_num(out: *u8, o: i64, v: i64) -> i64 { 86 var x: i64 = v 87 var p: i64 = o 88 if x < 0 { out[p] = 45 as u8; p = p + 1; x = 0 - x } 89 if x == 0 { out[p] = 48 as u8; return p + 1 } 90 var d: i64 = 0 91 var t: i64 = x 92 while t > 0 { d = d + 1; t = t / 10 } 93 var i: i64 = d - 1 94 while i >= 0 { out[p + i] = ((x % 10) + 48) as u8; x = x / 10; i = i - 1 } 95 return p + d 96} 97 98// ---- regime layer: SOVEREIGN SEG-STORE PLANE (knowledge/store/corridor-), read IN-PROCESS. 99// SOTA-from-the-first-byte: the corridor regime is DATA, not code -- add a country by writing a 100// plane row (nx_store_put), no reship. Row value = <cc> TAB <col2 0/1> TAB <eximclosed 0/1> TAB 101// <pref> TAB <kind> TAB <note>; sts_load emits rows NL-joined. The plane is AUTHORITATIVE; a small 102// embedded fallback keeps PRICING fail-safe if the plane is absent (rule-26 spirit: never silently 103// mis-price). Line/TAB parsing only -- no struct-offset surface. 104func lc_field0_eq(buf: *u8, n: i64, rs: i64, cc: *u8) -> i64 { 105 var p: i64 = rs 106 var m: i64 = 0 107 while cc[m] != (0 as u8) { 108 if p >= n { return 0 } 109 if buf[p] != cc[m] { return 0 } 110 p = p + 1; m = m + 1 111 } 112 if p >= n { return 1 } 113 if buf[p] == (9 as u8) { return 1 } 114 if buf[p] == (10 as u8) { return 1 } 115 return 0 116} 117func lc_extract(buf: *u8, n: i64, rs: i64, idx: i64, out: *u8, outcap: i64) -> i64 { 118 var fp: i64 = rs 119 var cf: i64 = 0 120 var oo: i64 = 0 121 var scan: i64 = 1 122 while scan == 1 { 123 if fp >= n { scan = 0 } else { 124 let ch: i64 = buf[fp] as i64 125 if ch == 10 { scan = 0 } else { 126 if ch == 9 { 127 if cf == idx { scan = 0 } else { cf = cf + 1; fp = fp + 1 } 128 } else { 129 if cf == idx { if oo < outcap - 1 { out[oo] = ch as u8; oo = oo + 1 } } 130 fp = fp + 1 131 } 132 } 133 } 134 } 135 out[oo] = 0 as u8 136 return oo 137} 138func lc_field(buf: *u8, n: i64, cc: *u8, idx: i64, out: *u8, outcap: i64) -> i64 { 139 var i: i64 = 0 140 while i < n { 141 if lc_field0_eq(buf, n, i, cc) == 1 { 142 lc_extract(buf, n, i, idx, out, outcap) 143 return 1 144 } 145 var adv: i64 = 1 146 while adv == 1 { 147 if i >= n { adv = 0 } else { 148 if buf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 149 } 150 } 151 } 152 return 0 153} 154func lc_load_regime(rbuf: *u8) -> i64 { return sts_load("knowledge/store/corridor-" as *u8, rbuf, LC_REGBUF) } 155func lc_is_col2(rbuf: *u8, rn: i64, cc: *u8) -> i64 { 156 let tmp: *u8 = sys_mmap(64) 157 if lc_field(rbuf, rn, cc, 1, tmp, 64) == 1 { return lc_atoi(tmp) } 158 if lc_eq(cc, "by" as *u8) == 1 { return 1 } 159 if lc_eq(cc, "ru" as *u8) == 1 { return 1 } 160 return 0 161} 162func lc_exim_closed(rbuf: *u8, rn: i64, cc: *u8) -> i64 { 163 let tmp: *u8 = sys_mmap(64) 164 if lc_field(rbuf, rn, cc, 2, tmp, 64) == 1 { return lc_atoi(tmp) } 165 if lc_eq(cc, "by" as *u8) == 1 { return 1 } 166 if lc_eq(cc, "ru" as *u8) == 1 { return 1 } 167 return 0 168} 169 170// ---- ORIGIN + ELIGIBILITY: all three read the SAME corridor plane the pricer already loads. 171// Row = <cc> TAB <col2> TAB <eximclosed> TAB <pref> TAB <kind> TAB <note> => field 3=pref, 4=kind. 172// DOMESTIC origin never clears customs: no duty, no 232/301/122, no MPF, no HMF (debt 1786039627). 173func lc_is_domestic(rbuf: *u8, rn: i64, cc: *u8) -> i64 { 174 let tmp: *u8 = sys_mmap(256) 175 if lc_field(rbuf, rn, cc, 4, tmp, 256) == 1 { if lc_eq(tmp, "domestic" as *u8) == 1 { return 1 } } 176 if lc_eq(cc, "us" as *u8) == 1 { return 1 } 177 return 0 178} 179// PREFERENCE ELIGIBILITY IS PER-CORRIDOR DATA, NOT A CALLER ASSERTION. A PNTR-suspended (Column 2) 180// country is ineligible for AGOA/USMCA/GSP BY CONSTRUCTION -- letting pref reach the col2 rate WAS 181// the sanctions bypass (debt 1786039611): pref=1 priced Russia and Belarus duty-free. 182func lc_pref_ok(rbuf: *u8, rn: i64, cc: *u8, col2f: i64) -> i64 { 183 if col2f == 1 { return 0 } 184 let tmp: *u8 = sys_mmap(256) 185 if lc_field(rbuf, rn, cc, 3, tmp, 256) == 0 { return 0 } 186 if tmp[0] == (0 as u8) { return 0 } 187 if lc_eq(tmp, "none" as *u8) == 1 { return 0 } 188 return 1 189} 190// Section 301 is a COUNTRY-SPECIFIC measure. Applying the caller's single rate to EVERY corridor 191// turned rank into a TIE-GENERATOR (debt 1786039627). Data-driven: plane field 6 (s301ok) when the 192// row carries one, else the kind marker the catalog already publishes. 193func lc_s301_applies(rbuf: *u8, rn: i64, cc: *u8) -> i64 { 194 let tmp: *u8 = sys_mmap(256) 195 if lc_field(rbuf, rn, cc, 6, tmp, 256) == 1 { if tmp[0] != (0 as u8) { return lc_atoi(tmp) } } 196 if lc_field(rbuf, rn, cc, 4, tmp, 256) == 1 { if lc_eq(tmp, "high-tariff-301" as *u8) == 1 { return 1 } } 197 return 0 198} 199 200// core compute -> fills out[] with the JSON, returns length. Also stashes 6 key integers in k[] for the gate. 201func lc_calc(out: *u8, cc: *u8, fob: i64, freight: i64, ins: i64, units: i64, col1: i64, col2: i64, s232: i64, s301: i64, price: i64, ocean: i64, s122: i64, pref: i64, other: i64, k: *i64) -> i64 { 202 let rbuf: *u8 = sys_mmap(LC_REGBUF) 203 let rn: i64 = lc_load_regime(rbuf) 204 let col2f: i64 = lc_is_col2(rbuf, rn, cc) 205 let domf: i64 = lc_is_domestic(rbuf, rn, cc) 206 var rate: i64 = col1 207 if col2f == 1 { rate = col2 } 208 if pref == 1 { if lc_pref_ok(rbuf, rn, cc, col2f) == 1 { rate = 0 } } 209 var base: i64 = fob * rate / LC_MAGIC_10000 210 var d232: i64 = fob * s232 / LC_MAGIC_10000 211 var d301: i64 = 0 212 if lc_s301_applies(rbuf, rn, cc) == 1 { d301 = fob * s301 / LC_MAGIC_10000 } 213 var d122: i64 = 0 214 if s122 == 1 { d122 = fob * LC_S122_BPS / LC_MAGIC_10000 } 215 var mpf: i64 = fob * LC_MPF_PPM / LC_MAGIC_1000000 216 if mpf < LC_MPF_MIN_C { mpf = LC_MPF_MIN_C } 217 if mpf > LC_MPF_MAX_C { mpf = LC_MPF_MAX_C } 218 var hmf: i64 = 0 219 if ocean == 1 { hmf = fob * LC_HMF_PPM / LC_MAGIC_1000000 } 220 // DOMESTIC ORIGIN CLEARS NO CUSTOMS. MPF and HMF are CBP ENTRY fees, and MPF carries a statutory 221 // FLOOR -- so charging them on a us-origin run added a FIXED phantom cost, regressive on exactly 222 // the small pilot runs a new product actually does, and it biased every sourcing decision against 223 // domestic supply. Zero the whole customs stack when the origin is domestic (debt 1786039627). 224 if domf == 1 { base = 0; d232 = 0; d301 = 0; d122 = 0; mpf = 0; hmf = 0 } 225 let duty: i64 = base + d232 + d301 + d122 + mpf + hmf 226 let cif: i64 = fob + freight + ins 227 let landed: i64 = cif + duty + other 228 let revenue: i64 = price * units 229 let margin: i64 = revenue - landed 230 var mperm: i64 = 0 231 if revenue > 0 { mperm = margin * 1000 / revenue } 232 var perunit: i64 = 0 233 if units > 0 { perunit = landed / units } 234 var burdenperm: i64 = 0 235 if fob > 0 { burdenperm = duty * 1000 / fob } 236 k[0] = base; k[1] = mpf; k[2] = hmf; k[3] = duty; k[4] = landed; k[5] = mperm; k[6] = d301; k[7] = domf 237 var colnum: i64 = 1 238 if col2f == 1 { colnum = 2 } 239 var o: i64 = 0 240 o = lc_lit(out, o, "{\"country\":\"" as *u8) 241 o = lc_lit(out, o, cc) 242 o = lc_lit(out, o, "\",\"column\":" as *u8) 243 o = lc_num(out, o, colnum) 244 o = lc_lit(out, o, ",\"duty_rate_bps\":" as *u8) 245 o = lc_num(out, o, rate) 246 o = lc_lit(out, o, ",\"base_duty_c\":" as *u8) 247 o = lc_num(out, o, base) 248 o = lc_lit(out, o, ",\"s232_c\":" as *u8) 249 o = lc_num(out, o, d232) 250 o = lc_lit(out, o, ",\"s301_c\":" as *u8) 251 o = lc_num(out, o, d301) 252 o = lc_lit(out, o, ",\"s122_c\":" as *u8) 253 o = lc_num(out, o, d122) 254 o = lc_lit(out, o, ",\"mpf_c\":" as *u8) 255 o = lc_num(out, o, mpf) 256 o = lc_lit(out, o, ",\"hmf_c\":" as *u8) 257 o = lc_num(out, o, hmf) 258 o = lc_lit(out, o, ",\"duty_total_c\":" as *u8) 259 o = lc_num(out, o, duty) 260 o = lc_lit(out, o, ",\"cif_c\":" as *u8) 261 o = lc_num(out, o, cif) 262 o = lc_lit(out, o, ",\"landed_c\":" as *u8) 263 o = lc_num(out, o, landed) 264 o = lc_lit(out, o, ",\"per_unit_c\":" as *u8) 265 o = lc_num(out, o, perunit) 266 o = lc_lit(out, o, ",\"revenue_c\":" as *u8) 267 o = lc_num(out, o, revenue) 268 o = lc_lit(out, o, ",\"margin_c\":" as *u8) 269 o = lc_num(out, o, margin) 270 o = lc_lit(out, o, ",\"margin_permil\":" as *u8) 271 o = lc_num(out, o, mperm) 272 o = lc_lit(out, o, ",\"duty_burden_permil\":" as *u8) 273 o = lc_num(out, o, burdenperm) 274 o = lc_lit(out, o, ",\"exim\":\"" as *u8) 275 if lc_exim_closed(rbuf, rn, cc) == 1 { o = lc_lit(out, o, "closed" as *u8) } else { o = lc_lit(out, o, "open" as *u8) } 276 o = lc_lit(out, o, "\",\"pntr\":\"" as *u8) 277 if col2f == 1 { o = lc_lit(out, o, "suspended" as *u8) } else { o = lc_lit(out, o, "normal" as *u8) } 278 o = lc_lit(out, o, "\",\"origin\":\"" as *u8) 279 if domf == 1 { o = lc_lit(out, o, "domestic" as *u8) } else { o = lc_lit(out, o, "import" as *u8) } 280 o = lc_lit(out, o, "\",\"band\":\"" as *u8) 281 if mperm >= 300 { o = lc_lit(out, o, "strong" as *u8) } else { if mperm >= 180 { o = lc_lit(out, o, "viable" as *u8) } else { if mperm >= 80 { o = lc_lit(out, o, "thin" as *u8) } else { if mperm > 0 { o = lc_lit(out, o, "marginal" as *u8) } else { o = lc_lit(out, o, "underwater" as *u8) } } } } 282 o = lc_lit(out, o, "\"}\n" as *u8) 283 out[o] = 0 as u8 284 return o 285} 286 287func lc_selftest() -> i64 { 288 let out: *u8 = sys_mmap(LC_MAGIC_4096) 289 let k: *i64 = sys_mmap(64) as *i64 290 var pass: i64 = 0 291 var total: i64 = 0 292 // Reference shipment (matches /trade.html Belarus default): fob $40,000; freight $3,800; ins $420; 293 // 2000 units; col1 3.7% col2 35%; ocean; s122 OFF (expired); other $1,835; price $38/unit. 294 lc_calc(out, "by" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 1, 0, 0, LC_MAGIC_183500, k) 295 total = total + 1; if k[0] == LC_MAGIC_1400000 { pass = pass + 1 } // base duty = col2 35% of $40k = $14,000 296 total = total + 1; if k[1] == LC_MAGIC_13856 { pass = pass + 1 } // MPF = 0.LC_MAGIC_3464% of $40k = $138.56 297 total = total + 1; if k[2] == LC_MAGIC_5000 { pass = pass + 1 } // HMF = 0.125% of $40k = $50.00 298 total = total + 1; if k[3] == LC_MAGIC_1418856 { pass = pass + 1 } // duty total = $14,188.56 299 total = total + 1; if k[4] == LC_MAGIC_6024356 { pass = pass + 1 } // landed = $60,243.56 300 total = total + 1; if k[5] == 207 { pass = pass + 1 } // margin = 207 permil (20.7%) 301 // NEGATIVE/REGIME control: SAME shipment from Central Asia (uz) -> Column 1, base duty collapses 302 // 35% -> 3.7% = $1,480 (proves the PNTR column switch is the load-bearing regime logic). 303 lc_calc(out, "uz" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 1, 0, 0, LC_MAGIC_183500, k) 304 total = total + 1; if k[0] == LC_MAGIC_148000 { pass = pass + 1 } // base = col1 3.7% of $40k = $1,480 305 total = total + 1; if k[3] == LC_MAGIC_166856 { pass = pass + 1 } // duty = LC_MAGIC_148000+LC_MAGIC_13856+LC_MAGIC_5000 306 // ---- ORIGIN + ELIGIBILITY TEETH (2026-08-06, shipped WITH the fix for debts 1786039611 and 307 // 1786039627). The OLD pref control ran pref only on uz -- a Column-1 corridor whose own plane 308 // row says preference=none -- so it asserted the WRONG thing and defended NEITHER side of the 309 // switch it claimed to test. Every tooth below now ships with its OPPOSITE. 310 // T9 pref POSITIVE: mx is USMCA-eligible in the plane -> pref zeroes the base duty. 311 lc_calc(out, "mx" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 1, 0, 1, LC_MAGIC_183500, k) 312 total = total + 1; if k[0] == 0 { pass = pass + 1 } // pref zeroes base duty 313 // T10 pref NEGATIVE: cn has NO preference programme -> claiming one must NOT zero the duty. 314 lc_calc(out, "cn" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 1, 0, 1, LC_MAGIC_183500, k) 315 total = total + 1; if k[0] > 0 { pass = pass + 1 } 316 // T11 SANCTIONS-BYPASS REGRESSION: by is Column 2 (PNTR suspended) and is ineligible for ANY 317 // preference by construction -> the punitive rate must SURVIVE pref=1. Before the fix this 318 // returned 0 and every sanctioned corridor priced duty-free. 319 lc_calc(out, "by" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 1, 0, 1, LC_MAGIC_183500, k) 320 total = total + 1; if k[0] == LC_MAGIC_1400000 { pass = pass + 1 } 321 // T12/T13 DOMESTIC ORIGIN: a us-origin run clears no customs -> the WHOLE duty stack is zero 322 // (no phantom MPF floor) and the origin flag reads domestic. 323 lc_calc(out, "us" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 0, LC_MAGIC_3800, 0, 0, 0, LC_MAGIC_183500, k) 324 total = total + 1; if k[3] == 0 { pass = pass + 1 } 325 total = total + 1; if k[7] == 1 { pass = pass + 1 } 326 // T14/T15 SECTION 301 IS COUNTRY-SPECIFIC: the SAME rate on two corridors, OPPOSITE outcomes. 327 // This is the tooth that stops rank being a tie-generator. 328 lc_calc(out, "cn" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 370, LC_MAGIC_3800, 1, 0, 0, LC_MAGIC_183500, k) 329 total = total + 1; if k[6] > 0 { pass = pass + 1 } 330 lc_calc(out, "vn" as *u8, LC_MAGIC_4000000, LC_MAGIC_380000, LC_MAGIC_42000, LC_MAGIC_2000, 370, LC_MAGIC_3500, 0, 370, LC_MAGIC_3800, 1, 0, 0, LC_MAGIC_183500, k) 331 total = total + 1; if k[6] == 0 { pass = pass + 1 } 332 var o: i64 = 0 333 let line: *u8 = sys_mmap(256) 334 o = lc_lit(line, o, "SELFTEST " as *u8) 335 o = lc_num(line, o, pass) 336 o = lc_lit(line, o, "/" as *u8) 337 o = lc_num(line, o, total) 338 if pass == total { o = lc_lit(line, o, " VERDICT=GREEN (integer-exact; col2/col1 switch, per-corridor pref ELIGIBILITY, sanctions-bypass regression, domestic origin, country-scoped 301 -- each proven BOTH WAYS)\n" as *u8) } else { o = lc_lit(line, o, " VERDICT=RED\n" as *u8) } 339 line[o] = 0 as u8 340 lw(1, line) 341 if pass == total { return 0 } 342 return 1 343} 344 345// ---- corridor regime card: an agent's DISCOVERY primitive for scaling to many countries. 346// Encodes the operator's thesis (profit us + the local populace, not extractive) as `kind`: 347// developing-partner / fta-partner / developed / high-tariff-301 / sanctioned / sanctioned-easing. 348func lc_eq3(cc: *u8, a: *u8, b: *u8, c: *u8) -> i64 { 349 if lc_eq(cc, a) == 1 { return 1 } 350 if lc_eq(cc, b) == 1 { return 1 } 351 if lc_eq(cc, c) == 1 { return 1 } 352 return 0 353} 354func lc_corridor(out: *u8, cc: *u8) -> i64 { 355 let rbuf: *u8 = sys_mmap(LC_REGBUF) 356 let rn: i64 = lc_load_regime(rbuf) 357 var pref: *u8 = "none" as *u8 358 var kind: *u8 = "unmodeled" as *u8 359 var note: *u8 = "not in regime table; treat as Column 1 unless PNTR is lost -- verify" as *u8 360 if lc_eq(cc, "by" as *u8) == 1 { kind = "sanctioned-easing" as *u8; note = "PNTR suspended so Column 2; OFAC easing (potash delisted, bank GLs) but BIS still gates goods -- finance open, freight walled" as *u8 } 361 if lc_eq(cc, "ru" as *u8) == 1 { kind = "sanctioned" as *u8; note = "sanctions tightening; an enumerated CHIPS country of concern -- a Russia corridor threatens the NSF track" as *u8 } 362 if lc_eq3(cc, "uz" as *u8, "kz" as *u8, "kg" as *u8) == 1 { kind = "developing-partner" as *u8; note = "Central Asia; normal trade relations, no sanctions overlay -- fastest clean corridor" as *u8 } 363 if lc_eq3(cc, "ge" as *u8, "am" as *u8, "md" as *u8) == 1 { kind = "developing-partner" as *u8; note = "Caucasus/Moldova; EU-aligned, real SME export capacity, low diligence burden" as *u8 } 364 if lc_eq3(cc, "ke" as *u8, "gh" as *u8, "et" as *u8) == 1 { pref = "agoa" as *u8; kind = "developing-partner" as *u8; note = "AGOA duty-free window (lapses end of year) -- best margin, shortest runway; claim pref=1" as *u8 } 365 if lc_eq(cc, "eu" as *u8) == 1 { kind = "developed" as *u8; note = "15% all-in ceiling under the current EU-US arrangement -- predictable, bankable, margin-thin" as *u8 } 366 if lc_eq(cc, "cn" as *u8) == 1 { kind = "high-tariff-301" as *u8; note = "Section 301 high-rate outlier -- the extractive pattern this venture is defined against" as *u8 } 367 if lc_eq3(cc, "mx" as *u8, "ca" as *u8, "ca" as *u8) == 1 { pref = "usmca" as *u8; kind = "fta-partner" as *u8; note = "USMCA duty-free IF the goods originate under the rules of origin; claim pref=1" as *u8 } 368 let kbuf: *u8 = sys_mmap(256) 369 if lc_field(rbuf, rn, cc, 4, kbuf, 256) == 1 { 370 let pbuf: *u8 = sys_mmap(256) 371 let nbuf: *u8 = sys_mmap(LC_NOTEBUF) 372 lc_field(rbuf, rn, cc, 3, pbuf, 256) 373 lc_field(rbuf, rn, cc, 5, nbuf, LC_NOTEBUF) 374 pref = pbuf; kind = kbuf; note = nbuf 375 } 376 let col2f: i64 = lc_is_col2(rbuf, rn, cc) 377 var o: i64 = 0 378 o = lc_lit(out, o, "{\"country\":\"" as *u8) 379 o = lc_lit(out, o, cc) 380 o = lc_lit(out, o, "\",\"column\":" as *u8) 381 if col2f == 1 { o = lc_lit(out, o, "2" as *u8) } else { o = lc_lit(out, o, "1" as *u8) } 382 o = lc_lit(out, o, ",\"pntr\":\"" as *u8) 383 if col2f == 1 { o = lc_lit(out, o, "suspended" as *u8) } else { o = lc_lit(out, o, "normal" as *u8) } 384 o = lc_lit(out, o, "\",\"exim\":\"" as *u8) 385 if lc_exim_closed(rbuf, rn, cc) == 1 { o = lc_lit(out, o, "closed" as *u8) } else { o = lc_lit(out, o, "open" as *u8) } 386 o = lc_lit(out, o, "\",\"preference\":\"" as *u8) 387 o = lc_lit(out, o, pref) 388 o = lc_lit(out, o, "\",\"kind\":\"" as *u8) 389 o = lc_lit(out, o, kind) 390 o = lc_lit(out, o, "\",\"note\":\"" as *u8) 391 o = lc_lit(out, o, note) 392 o = lc_lit(out, o, "\"}\n" as *u8) 393 out[o] = 0 as u8 394 return o 395} 396 397// corridors: dump EVERY corridor in the plane as a JSON array -- the bulk discovery primitive so 398// an agent/workflow can pull the whole regime catalog in one call and rank corridors by kind/margin. 399func lc_corridors_all(out: *u8) -> i64 { 400 let rbuf: *u8 = sys_mmap(LC_REGBUF) 401 let rn: i64 = lc_load_regime(rbuf) 402 let cc: *u8 = sys_mmap(64) 403 var o: i64 = 0 404 out[o] = 91 as u8; o = o + 1 405 var i: i64 = 0 406 var first: i64 = 1 407 while i < rn { 408 lc_extract(rbuf, rn, i, 0, cc, 64) 409 if cc[0] != (0 as u8) { 410 if first == 0 { out[o] = 44 as u8; o = o + 1 } 411 first = 0 412 let tmp: *u8 = sys_mmap(LC_CARDBUF) 413 let tn: i64 = lc_corridor(tmp, cc) 414 var j: i64 = 0 415 while j < tn { if tmp[j] != (10 as u8) { out[o] = tmp[j]; o = o + 1 } j = j + 1 } 416 } 417 var adv: i64 = 1 418 while adv == 1 { 419 if i >= rn { adv = 0 } else { 420 if rbuf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 421 } 422 } 423 } 424 out[o] = 93 as u8; o = o + 1 425 out[o] = 10 as u8; o = o + 1 426 out[o] = 0 as u8 427 return o 428} 429 430// rank: price ONE shipment across EVERY corridor in the plane and return them SORTED by margin -- 431// the corridor-selection capstone: the operator's core question ('which corridors profit us') answered 432// automatically across the whole catalog in one call. Reuses lc_calc per corridor (regime from the plane). 433func lc_rank(out: *u8, fob: i64, freight: i64, ins: i64, units: i64, col1: i64, col2: i64, s232: i64, s301: i64, price: i64, ocean: i64, s122: i64, pref: i64, other: i64) -> i64 { 434 let rbuf: *u8 = sys_mmap(LC_REGBUF) 435 let rn: i64 = lc_load_regime(rbuf) 436 let names: *u8 = sys_mmap(LC_MAXC * 8) 437 let marg: *i64 = sys_mmap(LC_MAXC * 8) as *i64 438 let land: *i64 = sys_mmap(LC_MAXC * 8) as *i64 439 let idx: *i64 = sys_mmap(LC_MAXC * 8) as *i64 440 let scratch: *u8 = sys_mmap(LC_CARDBUF) 441 let k: *i64 = sys_mmap(64) as *i64 442 var cnt: i64 = 0 443 var i: i64 = 0 444 while i < rn { 445 if cnt < LC_MAXC { 446 let slot: *u8 = (names as i64 + cnt * 8) as *u8 447 lc_extract(rbuf, rn, i, 0, slot, 8) 448 if slot[0] != (0 as u8) { 449 lc_calc(scratch, slot, fob, freight, ins, units, col1, col2, s232, s301, price, ocean, s122, pref, other, k) 450 marg[cnt] = k[5] 451 land[cnt] = k[4] 452 idx[cnt] = cnt 453 cnt = cnt + 1 454 } 455 } 456 var adv: i64 = 1 457 while adv == 1 { 458 if i >= rn { adv = 0 } else { 459 if rbuf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 460 } 461 } 462 } 463 var a: i64 = 0 464 while a < cnt { 465 var best: i64 = a 466 var b: i64 = a + 1 467 while b < cnt { 468 if marg[idx[b]] > marg[idx[best]] { best = b } 469 b = b + 1 470 } 471 let t: i64 = idx[a]; idx[a] = idx[best]; idx[best] = t 472 a = a + 1 473 } 474 var o: i64 = 0 475 out[o] = 91 as u8; o = o + 1 476 var r: i64 = 0 477 while r < cnt { 478 let ii: i64 = idx[r] 479 if r > 0 { out[o] = 44 as u8; o = o + 1 } 480 o = lc_lit(out, o, "{\"country\":\"" as *u8) 481 o = lc_lit(out, o, (names as i64 + ii * 8) as *u8) 482 o = lc_lit(out, o, "\",\"margin_permil\":" as *u8) 483 o = lc_num(out, o, marg[ii]) 484 o = lc_lit(out, o, ",\"landed_c\":" as *u8) 485 o = lc_num(out, o, land[ii]) 486 o = lc_lit(out, o, ",\"band\":\"" as *u8) 487 let mp: i64 = marg[ii] 488 if mp >= 300 { o = lc_lit(out, o, "strong" as *u8) } else { if mp >= 180 { o = lc_lit(out, o, "viable" as *u8) } else { if mp >= 80 { o = lc_lit(out, o, "thin" as *u8) } else { if mp > 0 { o = lc_lit(out, o, "marginal" as *u8) } else { o = lc_lit(out, o, "underwater" as *u8) } } } } 489 o = lc_lit(out, o, "\"}" as *u8) 490 r = r + 1 491 } 492 out[o] = 93 as u8; o = o + 1 493 out[o] = 10 as u8; o = o + 1 494 out[o] = 0 as u8 495 return o 496} 497 498// emit_catalog: the engine SELF-PUBLISHES its plane catalog as a JSON file (atomic tmp+rename) so a 499// clockjob can keep /trade-corridors.json auto-fresh from the seg-store SSOT -- one sovereign call, no glue. 500func lc_emit_catalog(outpath: *u8) -> i64 { 501 let buf: *u8 = sys_mmap(LC_REGBUF) 502 var o: i64 = 0 503 o = lc_lit(buf, o, "{\"source\":\"knowledge/store/corridor- (sovereign seg-store plane, read in-process by nx_landed_cost)\",\"note\":\"US import corridor regime SSOT. Add/edit a corridor = one plane row, no reship. column 1=normal-trade-relations, 2=PNTR-suspended. Product duty rates are HTS-specific engine INPUTS, not here. Verify vs a broker + live HTS.\",\"corridors\":" as *u8) 504 let arr: *u8 = sys_mmap(LC_REGBUF) 505 let an: i64 = lc_corridors_all(arr) 506 var j: i64 = 0 507 while j < an { if arr[j] != (10 as u8) { buf[o] = arr[j]; o = o + 1 } j = j + 1 } 508 o = lc_lit(buf, o, "}\n" as *u8) 509 buf[o] = 0 as u8 510 let tmp: *u8 = sys_mmap(512) 511 var t: i64 = 0 512 t = lc_lit(tmp, t, outpath) 513 t = lc_lit(tmp, t, ".tmp" as *u8) 514 tmp[t] = 0 as u8 515 let fd: i64 = sys_openat_wr(tmp, 0x1a4) 516 if fd < 0 { return 0 - 1 } 517 sys_write(fd, buf, o) 518 sys_close(fd) 519 sys_renameat(tmp, outpath) 520 return o 521} 522 523func main(argc: i64, argv: *i64) -> i64 { 524 if argc < 2 { 525 // no-arg = FRESHNESS-BEAT (the nx_model_card convention): self-publish the catalog to the 526 // docroot so a clockjob keeps /trade-corridors.json auto-fresh from the plane. Verbs below 527 // (calc/corridor/corridors/rank/emit_catalog/selftest) are the explicit agent surface. 528 let en: i64 = lc_emit_catalog("sites/nishifamily/trade-corridors.json" as *u8) 529 if en < 0 { lw(2, "CATALOG-EMIT-FAIL\n" as *u8); return 3 } 530 lw(1, "CATALOG-EMITTED (freshness-beat; no-arg default)\n" as *u8) 531 return 0 532 } 533 let verb: *u8 = argv[1] as *u8 534 if lc_eq(verb, "selftest" as *u8) == 1 { return lc_selftest() } 535 if lc_eq(verb, "corridors" as *u8) == 1 { 536 let out: *u8 = sys_mmap(LC_REGBUF) 537 lc_corridors_all(out) 538 lw(1, out) 539 return 0 540 } 541 if lc_eq(verb, "emit_catalog" as *u8) == 1 { 542 if argc < 3 { lw(2, "emit_catalog needs an out path: nx_landed_cost emit_catalog <path>\n" as *u8); return 2 } 543 let n: i64 = lc_emit_catalog(argv[2] as *u8) 544 if n < 0 { lw(2, "CATALOG-EMIT-FAIL (open)\n" as *u8); return 3 } 545 lw(1, "CATALOG-EMITTED\n" as *u8) 546 return 0 547 } 548 if lc_eq(verb, "rank" as *u8) == 1 { 549 if argc < 15 { lw(2, "rank needs 13 args: <fob_c> <freight_c> <ins_c> <units> <col1bps> <col2bps> <s232bps> <s301bps> <unitprice_c> <ocean01> <s122_01> <pref01> <other_c>\n" as *u8); return 2 } 550 let out: *u8 = sys_mmap(LC_REGBUF) 551 lc_rank(out, lc_atoi(argv[2] as *u8), lc_atoi(argv[3] as *u8), lc_atoi(argv[4] as *u8), lc_atoi(argv[5] as *u8), lc_atoi(argv[6] as *u8), lc_atoi(argv[7] as *u8), lc_atoi(argv[8] as *u8), lc_atoi(argv[9] as *u8), lc_atoi(argv[10] as *u8), lc_atoi(argv[11] as *u8), lc_atoi(argv[12] as *u8), lc_atoi(argv[13] as *u8), lc_atoi(argv[14] as *u8)) 552 lw(1, out) 553 return 0 554 } 555 if lc_eq(verb, "corridor" as *u8) == 1 { 556 if argc < 3 { lw(2, "corridor needs a country code: nx_landed_cost corridor <cc>\n" as *u8); return 2 } 557 let out: *u8 = sys_mmap(LC_CARDBUF) 558 lc_corridor(out, argv[2] as *u8) 559 lw(1, out) 560 return 0 561 } 562 if lc_eq(verb, "calc" as *u8) == 1 { 563 if argc < 16 { lw(2, "calc needs 14 args: <cc> <fob_c> <freight_c> <ins_c> <units> <col1bps> <col2bps> <s232bps> <s301bps> <unitprice_c> <ocean01> <s122_01> <pref01> <other_c>\n" as *u8); return 2 } 564 let out: *u8 = sys_mmap(LC_MAGIC_4096) 565 let k: *i64 = sys_mmap(64) as *i64 566 lc_calc(out, argv[2] as *u8, lc_atoi(argv[3] as *u8), lc_atoi(argv[4] as *u8), lc_atoi(argv[5] as *u8), lc_atoi(argv[6] as *u8), lc_atoi(argv[7] as *u8), lc_atoi(argv[8] as *u8), lc_atoi(argv[9] as *u8), lc_atoi(argv[10] as *u8), lc_atoi(argv[11] as *u8), lc_atoi(argv[12] as *u8), lc_atoi(argv[13] as *u8), lc_atoi(argv[14] as *u8), lc_atoi(argv[15] as *u8), k) 567 lw(1, out) 568 return 0 569 } 570 lw(2, "usage: nx_landed_cost calc ... | selftest\n" as *u8) 571 return 2 572}