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