code wiki / _hdl_build / nx_landed_cost.nx

nx_landed_cost.nx source

↩ module page · 510 lines · 25392 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// core compute -> fills out[] with the JSON, returns length. Also stashes 6 key integers in k[] for the gate. 171func 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 { 172 let rbuf: *u8 = sys_mmap(LC_REGBUF) 173 let rn: i64 = lc_load_regime(rbuf) 174 let col2f: i64 = lc_is_col2(rbuf, rn, cc) 175 var rate: i64 = col1 176 if col2f == 1 { rate = col2 } 177 if pref == 1 { rate = 0 } 178 let base: i64 = fob * rate / LC_MAGIC_10000 179 let d232: i64 = fob * s232 / LC_MAGIC_10000 180 let d301: i64 = fob * s301 / LC_MAGIC_10000 181 var d122: i64 = 0 182 if s122 == 1 { d122 = fob * LC_S122_BPS / LC_MAGIC_10000 } 183 var mpf: i64 = fob * LC_MPF_PPM / LC_MAGIC_1000000 184 if mpf < LC_MPF_MIN_C { mpf = LC_MPF_MIN_C } 185 if mpf > LC_MPF_MAX_C { mpf = LC_MPF_MAX_C } 186 var hmf: i64 = 0 187 if ocean == 1 { hmf = fob * LC_HMF_PPM / LC_MAGIC_1000000 } 188 let duty: i64 = base + d232 + d301 + d122 + mpf + hmf 189 let cif: i64 = fob + freight + ins 190 let landed: i64 = cif + duty + other 191 let revenue: i64 = price * units 192 let margin: i64 = revenue - landed 193 var mperm: i64 = 0 194 if revenue > 0 { mperm = margin * 1000 / revenue } 195 var perunit: i64 = 0 196 if units > 0 { perunit = landed / units } 197 var burdenperm: i64 = 0 198 if fob > 0 { burdenperm = duty * 1000 / fob } 199 k[0] = base; k[1] = mpf; k[2] = hmf; k[3] = duty; k[4] = landed; k[5] = mperm 200 var colnum: i64 = 1 201 if col2f == 1 { colnum = 2 } 202 var o: i64 = 0 203 o = lc_lit(out, o, "{\"country\":\"" as *u8) 204 o = lc_lit(out, o, cc) 205 o = lc_lit(out, o, "\",\"column\":" as *u8) 206 o = lc_num(out, o, colnum) 207 o = lc_lit(out, o, ",\"duty_rate_bps\":" as *u8) 208 o = lc_num(out, o, rate) 209 o = lc_lit(out, o, ",\"base_duty_c\":" as *u8) 210 o = lc_num(out, o, base) 211 o = lc_lit(out, o, ",\"s232_c\":" as *u8) 212 o = lc_num(out, o, d232) 213 o = lc_lit(out, o, ",\"s301_c\":" as *u8) 214 o = lc_num(out, o, d301) 215 o = lc_lit(out, o, ",\"s122_c\":" as *u8) 216 o = lc_num(out, o, d122) 217 o = lc_lit(out, o, ",\"mpf_c\":" as *u8) 218 o = lc_num(out, o, mpf) 219 o = lc_lit(out, o, ",\"hmf_c\":" as *u8) 220 o = lc_num(out, o, hmf) 221 o = lc_lit(out, o, ",\"duty_total_c\":" as *u8) 222 o = lc_num(out, o, duty) 223 o = lc_lit(out, o, ",\"cif_c\":" as *u8) 224 o = lc_num(out, o, cif) 225 o = lc_lit(out, o, ",\"landed_c\":" as *u8) 226 o = lc_num(out, o, landed) 227 o = lc_lit(out, o, ",\"per_unit_c\":" as *u8) 228 o = lc_num(out, o, perunit) 229 o = lc_lit(out, o, ",\"revenue_c\":" as *u8) 230 o = lc_num(out, o, revenue) 231 o = lc_lit(out, o, ",\"margin_c\":" as *u8) 232 o = lc_num(out, o, margin) 233 o = lc_lit(out, o, ",\"margin_permil\":" as *u8) 234 o = lc_num(out, o, mperm) 235 o = lc_lit(out, o, ",\"duty_burden_permil\":" as *u8) 236 o = lc_num(out, o, burdenperm) 237 o = lc_lit(out, o, ",\"exim\":\"" as *u8) 238 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) } 239 o = lc_lit(out, o, "\",\"pntr\":\"" as *u8) 240 if col2f == 1 { o = lc_lit(out, o, "suspended" as *u8) } else { o = lc_lit(out, o, "normal" as *u8) } 241 o = lc_lit(out, o, "\",\"band\":\"" as *u8) 242 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) } } } } 243 o = lc_lit(out, o, "\"}\n" as *u8) 244 out[o] = 0 as u8 245 return o 246} 247 248func lc_selftest() -> i64 { 249 let out: *u8 = sys_mmap(LC_MAGIC_4096) 250 let k: *i64 = sys_mmap(64) as *i64 251 var pass: i64 = 0 252 var total: i64 = 0 253 // Reference shipment (matches /trade.html Belarus default): fob $40,000; freight $3,800; ins $420; 254 // 2000 units; col1 3.7% col2 35%; ocean; s122 OFF (expired); other $1,835; price $38/unit. 255 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) 256 total = total + 1; if k[0] == LC_MAGIC_1400000 { pass = pass + 1 } // base duty = col2 35% of $40k = $14,000 257 total = total + 1; if k[1] == LC_MAGIC_13856 { pass = pass + 1 } // MPF = 0.LC_MAGIC_3464% of $40k = $138.56 258 total = total + 1; if k[2] == LC_MAGIC_5000 { pass = pass + 1 } // HMF = 0.125% of $40k = $50.00 259 total = total + 1; if k[3] == LC_MAGIC_1418856 { pass = pass + 1 } // duty total = $14,188.56 260 total = total + 1; if k[4] == LC_MAGIC_6024356 { pass = pass + 1 } // landed = $60,243.56 261 total = total + 1; if k[5] == 207 { pass = pass + 1 } // margin = 207 permil (20.7%) 262 // NEGATIVE/REGIME control: SAME shipment from Central Asia (uz) -> Column 1, base duty collapses 263 // 35% -> 3.7% = $1,480 (proves the PNTR column switch is the load-bearing regime logic). 264 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) 265 total = total + 1; if k[0] == LC_MAGIC_148000 { pass = pass + 1 } // base = col1 3.7% of $40k = $1,480 266 total = total + 1; if k[3] == LC_MAGIC_166856 { pass = pass + 1 } // duty = LC_MAGIC_148000+LC_MAGIC_13856+LC_MAGIC_5000 267 // pref control: claim a preference (AGOA/USMCA) -> base duty 0 even on a col1 line 268 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, 1, LC_MAGIC_183500, k) 269 total = total + 1; if k[0] == 0 { pass = pass + 1 } // pref zeroes base duty 270 var o: i64 = 0 271 let line: *u8 = sys_mmap(256) 272 o = lc_lit(line, o, "SELFTEST " as *u8) 273 o = lc_num(line, o, pass) 274 o = lc_lit(line, o, "/" as *u8) 275 o = lc_num(line, o, total) 276 if pass == total { o = lc_lit(line, o, " VERDICT=GREEN (integer-exact vs hand arithmetic; col2/col1 switch + pref proven)\n" as *u8) } else { o = lc_lit(line, o, " VERDICT=RED\n" as *u8) } 277 line[o] = 0 as u8 278 lw(1, line) 279 if pass == total { return 0 } 280 return 1 281} 282 283// ---- corridor regime card: an agent's DISCOVERY primitive for scaling to many countries. 284// Encodes the operator's thesis (profit us + the local populace, not extractive) as `kind`: 285// developing-partner / fta-partner / developed / high-tariff-301 / sanctioned / sanctioned-easing. 286func lc_eq3(cc: *u8, a: *u8, b: *u8, c: *u8) -> i64 { 287 if lc_eq(cc, a) == 1 { return 1 } 288 if lc_eq(cc, b) == 1 { return 1 } 289 if lc_eq(cc, c) == 1 { return 1 } 290 return 0 291} 292func lc_corridor(out: *u8, cc: *u8) -> i64 { 293 let rbuf: *u8 = sys_mmap(LC_REGBUF) 294 let rn: i64 = lc_load_regime(rbuf) 295 var pref: *u8 = "none" as *u8 296 var kind: *u8 = "unmodeled" as *u8 297 var note: *u8 = "not in regime table; treat as Column 1 unless PNTR is lost -- verify" as *u8 298 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 } 299 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 } 300 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 } 301 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 } 302 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 } 303 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 } 304 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 } 305 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 } 306 let kbuf: *u8 = sys_mmap(256) 307 if lc_field(rbuf, rn, cc, 4, kbuf, 256) == 1 { 308 let pbuf: *u8 = sys_mmap(256) 309 let nbuf: *u8 = sys_mmap(LC_NOTEBUF) 310 lc_field(rbuf, rn, cc, 3, pbuf, 256) 311 lc_field(rbuf, rn, cc, 5, nbuf, LC_NOTEBUF) 312 pref = pbuf; kind = kbuf; note = nbuf 313 } 314 let col2f: i64 = lc_is_col2(rbuf, rn, cc) 315 var o: i64 = 0 316 o = lc_lit(out, o, "{\"country\":\"" as *u8) 317 o = lc_lit(out, o, cc) 318 o = lc_lit(out, o, "\",\"column\":" as *u8) 319 if col2f == 1 { o = lc_lit(out, o, "2" as *u8) } else { o = lc_lit(out, o, "1" as *u8) } 320 o = lc_lit(out, o, ",\"pntr\":\"" as *u8) 321 if col2f == 1 { o = lc_lit(out, o, "suspended" as *u8) } else { o = lc_lit(out, o, "normal" as *u8) } 322 o = lc_lit(out, o, "\",\"exim\":\"" as *u8) 323 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) } 324 o = lc_lit(out, o, "\",\"preference\":\"" as *u8) 325 o = lc_lit(out, o, pref) 326 o = lc_lit(out, o, "\",\"kind\":\"" as *u8) 327 o = lc_lit(out, o, kind) 328 o = lc_lit(out, o, "\",\"note\":\"" as *u8) 329 o = lc_lit(out, o, note) 330 o = lc_lit(out, o, "\"}\n" as *u8) 331 out[o] = 0 as u8 332 return o 333} 334 335// corridors: dump EVERY corridor in the plane as a JSON array -- the bulk discovery primitive so 336// an agent/workflow can pull the whole regime catalog in one call and rank corridors by kind/margin. 337func lc_corridors_all(out: *u8) -> i64 { 338 let rbuf: *u8 = sys_mmap(LC_REGBUF) 339 let rn: i64 = lc_load_regime(rbuf) 340 let cc: *u8 = sys_mmap(64) 341 var o: i64 = 0 342 out[o] = 91 as u8; o = o + 1 343 var i: i64 = 0 344 var first: i64 = 1 345 while i < rn { 346 lc_extract(rbuf, rn, i, 0, cc, 64) 347 if cc[0] != (0 as u8) { 348 if first == 0 { out[o] = 44 as u8; o = o + 1 } 349 first = 0 350 let tmp: *u8 = sys_mmap(LC_CARDBUF) 351 let tn: i64 = lc_corridor(tmp, cc) 352 var j: i64 = 0 353 while j < tn { if tmp[j] != (10 as u8) { out[o] = tmp[j]; o = o + 1 } j = j + 1 } 354 } 355 var adv: i64 = 1 356 while adv == 1 { 357 if i >= rn { adv = 0 } else { 358 if rbuf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 359 } 360 } 361 } 362 out[o] = 93 as u8; o = o + 1 363 out[o] = 10 as u8; o = o + 1 364 out[o] = 0 as u8 365 return o 366} 367 368// rank: price ONE shipment across EVERY corridor in the plane and return them SORTED by margin -- 369// the corridor-selection capstone: the operator's core question ('which corridors profit us') answered 370// automatically across the whole catalog in one call. Reuses lc_calc per corridor (regime from the plane). 371func 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 { 372 let rbuf: *u8 = sys_mmap(LC_REGBUF) 373 let rn: i64 = lc_load_regime(rbuf) 374 let names: *u8 = sys_mmap(LC_MAXC * 8) 375 let marg: *i64 = sys_mmap(LC_MAXC * 8) as *i64 376 let land: *i64 = sys_mmap(LC_MAXC * 8) as *i64 377 let idx: *i64 = sys_mmap(LC_MAXC * 8) as *i64 378 let scratch: *u8 = sys_mmap(LC_CARDBUF) 379 let k: *i64 = sys_mmap(64) as *i64 380 var cnt: i64 = 0 381 var i: i64 = 0 382 while i < rn { 383 if cnt < LC_MAXC { 384 let slot: *u8 = (names as i64 + cnt * 8) as *u8 385 lc_extract(rbuf, rn, i, 0, slot, 8) 386 if slot[0] != (0 as u8) { 387 lc_calc(scratch, slot, fob, freight, ins, units, col1, col2, s232, s301, price, ocean, s122, pref, other, k) 388 marg[cnt] = k[5] 389 land[cnt] = k[4] 390 idx[cnt] = cnt 391 cnt = cnt + 1 392 } 393 } 394 var adv: i64 = 1 395 while adv == 1 { 396 if i >= rn { adv = 0 } else { 397 if rbuf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 398 } 399 } 400 } 401 var a: i64 = 0 402 while a < cnt { 403 var best: i64 = a 404 var b: i64 = a + 1 405 while b < cnt { 406 if marg[idx[b]] > marg[idx[best]] { best = b } 407 b = b + 1 408 } 409 let t: i64 = idx[a]; idx[a] = idx[best]; idx[best] = t 410 a = a + 1 411 } 412 var o: i64 = 0 413 out[o] = 91 as u8; o = o + 1 414 var r: i64 = 0 415 while r < cnt { 416 let ii: i64 = idx[r] 417 if r > 0 { out[o] = 44 as u8; o = o + 1 } 418 o = lc_lit(out, o, "{\"country\":\"" as *u8) 419 o = lc_lit(out, o, (names as i64 + ii * 8) as *u8) 420 o = lc_lit(out, o, "\",\"margin_permil\":" as *u8) 421 o = lc_num(out, o, marg[ii]) 422 o = lc_lit(out, o, ",\"landed_c\":" as *u8) 423 o = lc_num(out, o, land[ii]) 424 o = lc_lit(out, o, ",\"band\":\"" as *u8) 425 let mp: i64 = marg[ii] 426 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) } } } } 427 o = lc_lit(out, o, "\"}" as *u8) 428 r = r + 1 429 } 430 out[o] = 93 as u8; o = o + 1 431 out[o] = 10 as u8; o = o + 1 432 out[o] = 0 as u8 433 return o 434} 435 436// emit_catalog: the engine SELF-PUBLISHES its plane catalog as a JSON file (atomic tmp+rename) so a 437// clockjob can keep /trade-corridors.json auto-fresh from the seg-store SSOT -- one sovereign call, no glue. 438func lc_emit_catalog(outpath: *u8) -> i64 { 439 let buf: *u8 = sys_mmap(LC_REGBUF) 440 var o: i64 = 0 441 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) 442 let arr: *u8 = sys_mmap(LC_REGBUF) 443 let an: i64 = lc_corridors_all(arr) 444 var j: i64 = 0 445 while j < an { if arr[j] != (10 as u8) { buf[o] = arr[j]; o = o + 1 } j = j + 1 } 446 o = lc_lit(buf, o, "}\n" as *u8) 447 buf[o] = 0 as u8 448 let tmp: *u8 = sys_mmap(512) 449 var t: i64 = 0 450 t = lc_lit(tmp, t, outpath) 451 t = lc_lit(tmp, t, ".tmp" as *u8) 452 tmp[t] = 0 as u8 453 let fd: i64 = sys_openat_wr(tmp, 0x1a4) 454 if fd < 0 { return 0 - 1 } 455 sys_write(fd, buf, o) 456 sys_close(fd) 457 sys_renameat(tmp, outpath) 458 return o 459} 460 461func main(argc: i64, argv: *i64) -> i64 { 462 if argc < 2 { 463 // no-arg = FRESHNESS-BEAT (the nx_model_card convention): self-publish the catalog to the 464 // docroot so a clockjob keeps /trade-corridors.json auto-fresh from the plane. Verbs below 465 // (calc/corridor/corridors/rank/emit_catalog/selftest) are the explicit agent surface. 466 let en: i64 = lc_emit_catalog("sites/nishifamily/trade-corridors.json" as *u8) 467 if en < 0 { lw(2, "CATALOG-EMIT-FAIL\n" as *u8); return 3 } 468 lw(1, "CATALOG-EMITTED (freshness-beat; no-arg default)\n" as *u8) 469 return 0 470 } 471 let verb: *u8 = argv[1] as *u8 472 if lc_eq(verb, "selftest" as *u8) == 1 { return lc_selftest() } 473 if lc_eq(verb, "corridors" as *u8) == 1 { 474 let out: *u8 = sys_mmap(LC_REGBUF) 475 lc_corridors_all(out) 476 lw(1, out) 477 return 0 478 } 479 if lc_eq(verb, "emit_catalog" as *u8) == 1 { 480 if argc < 3 { lw(2, "emit_catalog needs an out path: nx_landed_cost emit_catalog <path>\n" as *u8); return 2 } 481 let n: i64 = lc_emit_catalog(argv[2] as *u8) 482 if n < 0 { lw(2, "CATALOG-EMIT-FAIL (open)\n" as *u8); return 3 } 483 lw(1, "CATALOG-EMITTED\n" as *u8) 484 return 0 485 } 486 if lc_eq(verb, "rank" as *u8) == 1 { 487 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 } 488 let out: *u8 = sys_mmap(LC_REGBUF) 489 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)) 490 lw(1, out) 491 return 0 492 } 493 if lc_eq(verb, "corridor" as *u8) == 1 { 494 if argc < 3 { lw(2, "corridor needs a country code: nx_landed_cost corridor <cc>\n" as *u8); return 2 } 495 let out: *u8 = sys_mmap(LC_CARDBUF) 496 lc_corridor(out, argv[2] as *u8) 497 lw(1, out) 498 return 0 499 } 500 if lc_eq(verb, "calc" as *u8) == 1 { 501 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 } 502 let out: *u8 = sys_mmap(LC_MAGIC_4096) 503 let k: *i64 = sys_mmap(64) as *i64 504 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) 505 lw(1, out) 506 return 0 507 } 508 lw(2, "usage: nx_landed_cost calc ... | selftest\n" as *u8) 509 return 2 510}