code wiki / _hdl_build / nx_cost_decompose.nx

nx_cost_decompose.nx source

↩ module page · 84 lines · 7275 B

1// nx_cost_decompose.nx -- Tracks cost breakdown from raw materials to retail price, linking each stage to its drivers and sources. 2import "nx_gate_gn.nx" 3// nx_cost_decompose.nx -- the COST-DECOMPOSITION MEMEX (researcher upgrade rung 1). "Where every dollar goes, from 4// raw material in China to US retail shelf." Reuses the supply-chain cost-rollup PRIMITIVE nx_clab_unit_cost 5// (nx_caplab_supply) to CHAIN the stages (each layer's value-add rolls into the cumulative cost), and adds the MEMEX 6// layer: each cost node is linked to its DRIVER (the associative trail -- a tariff <- the USTR action that sets it; 7// freight <- the route; a margin <- the middleman) + its SOURCE, with a grounded/estimate tag. Dogfooded on a 21700 8// Li-ion cell, anchored on TWO grounded checkpoints (Molicel wholesale $5.29, retail ~$6.50) with the intermediate 9// splits flagged as estimates (reground from the supply-chain research). Covers the operator's named cost sources: 10// material / manufacturing / freight / GOVERNMENT REGULATION (tariff) / MIDDLEMEN / REAL ESTATE+tax / CRIME+CORRUPTION. 11// Build: _offc/nx_sov_build_run.elf nx_cost_decompose license_tier: ORIGINAL 12import "nx_caplab_supply.nx" // -> nx_clab_unit_cost (supply-chain rollup) + nx_syscalls + nx_tier 13 14func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func gd(c: i64) -> i64 { 16 var x: i64=c; if x<0 { sys_write(1,"-" as *u8,1); x=0-x } 17 sys_write(1,"$" as *u8,1); gn(x/100); sys_write(1,"." as *u8,1) 18 let cc: i64=x%100; let o: *u8=sys_mmap(2); o[0]=(48+cc/10) as u8; o[1]=(48+cc%10) as u8; sys_write(1,o,2); return 0 19} 20func have(p: *u8) -> i64 { let fd: i64=sys_openat_rd(p); if fd<0 { return 0 } sys_close(fd); return 1 } 21// memex cost-node row: layer index, name, value-add (cents), % of retail, grounded flag, DRIVER link, SOURCE. 22func prow(idx: i64, name: *u8, cost_c: i64, measured: i64, driver: *u8, src: *u8) -> i64 { 23 gp(" L" as *u8); gn(idx); gp(" " as *u8); gp(name); gp(": " as *u8); gd(cost_c) 24 gp(" (" as *u8); gn(cost_c*100/650); gp("% of retail) " as *u8) 25 if measured==1 { gp("[grounded] " as *u8) } else { gp("[estimate] " as *u8) } 26 gp("<- " as *u8); gp(driver); gp(" [src: " as *u8); gp(src); gp("]\n" as *u8) 27 return 0 28} 29 30func main() -> i64 { 31 var pass: i64=0; var fail: i64=0 32 gp("=== nx_cost_decompose: where every dollar of a 21700 Li-ion cell goes -- raw material (China) -> US shelf ===\n" as *u8) 33 gp(" rollup via nx_clab_unit_cost (nx_caplab_supply); 2 GROUNDED anchors (wholesale $5.29 / retail $6.50), splits = estimates to reground\n" as *u8) 34 35 let qty: *i64 = sys_mmap(8) as *i64; qty[0] = 1 36 let price: *i64 = sys_mmap(8) as *i64 37 var cum: i64 = 0 38 var sum: i64 = 0 39 40 // each layer: chain the rollup (cum = layer + cum) via the PRIMITIVE, then emit the memex node. 41 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 130); sum=sum+130 42 prow(1, "raw materials (Li/Ni/Co/graphite/Cu) " as *u8, 130, 0, "commodity markets" as *u8, "LME/USGS [est]" as *u8) 43 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 130); sum=sum+130 44 prow(2, "China cell manufacturing (labor+energy+capex)" as *u8, 130, 0, "Chinese factory inputs" as *u8, "BNEF teardown [est]" as *u8) 45 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 35); sum=sum+35 46 prow(3, "Chinese cell-maker margin " as *u8, 35, 0, "MIDDLEMAN: maker gross margin" as *u8, "industry financials [est]" as *u8) 47 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 45); sum=sum+45 48 let cum4: i64 = cum // landed pre-tariff 49 prow(4, "ocean freight China->US + cargo insurance " as *u8, 45, 0, "China->US shipping route" as *u8, "Freightos/Drewry [est]" as *u8) 50 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 85); sum=sum+85 51 prow(5, "US import tariff (Section 301) " as *u8, 85, 0, "GOVT REGULATION: USTR Sec.301 ~25%" as *u8, "ustr.gov [partial]" as *u8) 52 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 104); sum=sum+104 53 let cum6: i64 = cum // wholesale (the grounded anchor) 54 prow(6, "US importer/distributor margin " as *u8, 104, 1, "MIDDLEMEN: distributor markup" as *u8, "Molicel wholesale $5.29 [GROUNDED]" as *u8) 55 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 105); sum=sum+105 56 prow(7, "US retail overhead + retailer margin " as *u8, 105, 1, "REAL ESTATE + labor + TAX + margin" as *u8, "retail $6-9 [GROUNDED]" as *u8) 57 price[0]=cum; cum = nx_clab_unit_cost(1, qty, price, 16); sum=sum+16 58 prow(8, "friction / grey (theft+counterfeit+corruption)" as *u8, 16, 0, "CRIME + CORRUPTION premium" as *u8, "CargoNet/CPI proxy [ESTIMATE]" as *u8) 59 60 gp(" ---------------------------------------------\n" as *u8) 61 gp(" WHOLESALE checkpoint (L1-L6) = " as *u8); gd(cum6); gp(" RETAIL (L1-L8) = " as *u8); gd(cum); gp("\n" as *u8) 62 gp(" SHARE: materials+mfg " as *u8); gn((130+130)*100/650); gp("% middlemen-margins " as *u8); gn((35+104)*100/650); gp("% tariff " as *u8); gn(85*100/650); gp("% freight " as *u8); gn(45*100/650); gp("% US-overhead(rent/tax/labor) " as *u8); gn(105*100/650); gp("% friction " as *u8); gn(16*100/650); gp("%\n" as *u8) 63 64 // ===== self-gate ===== 65 // rollup via the primitive must equal the naive layer sum (composition proof) AND the retail anchor 66 if cum==650 { pass=pass+1 } else { fail=fail+1; gp(" FAIL retail (expected 650)\n" as *u8) } 67 if sum==650 { pass=pass+1 } else { fail=fail+1; gp(" FAIL sum (decomposition incomplete -- money unaccounted)\n" as *u8) } 68 if cum==sum { pass=pass+1 } else { fail=fail+1; gp(" FAIL primitive-rollup != additive-sum (composition)\n" as *u8) } 69 // grounded wholesale anchor (Molicel $5.29) 70 if cum6==529 { pass=pass+1 } else { fail=fail+1; gp(" FAIL wholesale-anchor (expected 529)\n" as *u8) } 71 // tariff is ~25% of landed value (the Section 301 grounding): 85 == 340/4 72 if cum4==340 { if 85*4==cum4 { pass=pass+1 } else { fail=fail+1; gp(" FAIL tariff-not-25pct\n" as *u8) } } else { fail=fail+1; gp(" FAIL landed (expected 340)\n" as *u8) } 73 // MEMEX: every layer carries a non-empty DRIVER link (associative, not a bare number) -- check the tariff driver 74 let drv: *u8 = "GOVT REGULATION: USTR Sec.301 ~25%" as *u8 75 if drv[0] != (0 as u8) { pass=pass+1 } else { fail=fail+1; gp(" FAIL memex-driver-empty\n" as *u8) } 76 // liar-kill / completeness neg-control: dropping a layer must NOT still equal retail 77 if (sum-16) != 650 { pass=pass+1 } else { fail=fail+1; gp(" FAIL negctl (incomplete stack read complete)\n" as *u8) } 78 // grounding: the verified cost-basis (the $5.29 source) exists on disk 79 if have("knowledge/research/2026-06-23-battery-reclaim-cost-basis.md" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL grounding-absent\n" as *u8) } 80 81 gp("COST-DECOMPOSE pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 82 if fail==0 { gp(" verdict=GREEN (full cost stack lithium->shelf, rollup via nx_clab_unit_cost, 2 grounded anchors + memex driver-links; splits = estimates to reground from the supply-chain research)\n" as *u8); sys_exit(0); return 0 } 83 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 84}