code wiki / _hdl_build / nx_prodladder.nx

nx_prodladder.nx source

↩ module page · 531 lines · 24189 B

1// nx_prodladder.nx -- SOVEREIGN PRODUCTION-CAPABILITY LADDER (rent -> lease -> buy -> enterprise). 2// 3// Operator, 2026-08-06: "when we get these production capabilities up for these products first 4// rent then buy or cheap then enterprise ... lets get it a reusable process." 5// 6// WHY THIS ORGAN EXISTS. The estate can already price a SHIPMENT (nx_landed_cost, 15/15 GREEN) and 7// score a PRODUCT (nx_product_score). It could not answer the question that actually gates a 8// physical product: AT WHAT VOLUME DOES IT STOP BEING RIGHT TO RENT AND START BEING RIGHT TO BUY? 9// Searched before building -- nx_prim_query over 855 registered primitives returns 0 matches for 10// `capex` and `tooling`, and `production` returns only e-discovery gates. The rung was missing. 11// 12// THE MODEL. Every acquisition tier is the same shape -- a one-time cost amortised over its life, 13// a standing annual cost, and a per-unit cost -- so tiers are COMPARABLE and the ladder is just 14// argmin over them at a given volume: 15// annual(V) = (capex + tooling) / life_years + fixed_annual + per_unit * V 16// The step-up point between two tiers is where those lines cross: 17// V* = ceil( (standingA - standingB) / (per_unitB - per_unitA) ) for per_unitA > per_unitB 18// That crossover IS the reusable process: it turns "when do we buy the machine?" from a judgement 19// call into an arithmetic one, and it is the SAME arithmetic for a rubber dog ball, an injection 20// moulded enclosure, or a PCB run. The ladder is generic; only the plane rows are product-specific. 21// 22// INTEGER-EXACT, NO FLOAT (sovereign law): money in CENTS, volumes in UNITS. Every figure is a pure 23// integer transform of the inputs -- reproducible and hand-checkable, no rounding drift. 24// 25// ★★THE TIER ECONOMICS ARE DATA, NOT CODE. They live in the seg-store plane 26// `knowledge/store/prodladder-`, one row per <process>_<tier>. Adding a process is a plane write 27// (nx_store_put), never a reship -- rule 11, no magic numbers buried in a binary. 28// 29// ★★★AND IF THE PLANE HAS NO ROW, THIS ORGAN REFUSES. It does NOT fall back to a plausible 30// default. A capex ladder that invents its own capex is worse than no ladder at all: it produces a 31// confident break-even volume that someone will spend real money against. nx_product_score already 32// states the estate's law -- "NO fabricated market/cost figures here (operator no-fake-numbers 33// law)" -- and a refusal is the only honest answer to a question we lack the data to answer. 34// The selftest therefore proves the ARITHMETIC on clearly-labelled SYNTHETIC fixtures, and proves 35// the REFUSAL on a process the plane does not carry. Fixtures prove math; the plane carries facts. 36// 37// PLANE ROW (TAB-separated, field 0 = key): 38// <process>_<tier> <tier_name> <capex_c> <tooling_c> <fixed_annual_c> <per_unit_c> 39// <life_years> <max_annual_units|0=unbounded> <note> 40// tier suffixes, cheap -> enterprise: _t0 rent/service-bureau _t1 lease _t2 buy _t3 line 41// 42// VERBS: 43// nx_prodladder calc <process> <annual_units> -> JSON: per-tier annual + per-unit, chosen tier 44// nx_prodladder ramp <process> <v1> [v2..v6] -> the LADDER: which tier at each volume + steps 45// nx_prodladder selftest -> arithmetic + refusal gate -> VERDICT 46// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 47import "nx_syscalls.nx" 48import "nx_seg_store.nx" 49import "nx_store_seed_lib.nx" // provides sts_load (the seg-store plane reader nx_landed_cost uses) 50const PL_MAGIC_4000000: i64 = 4000000 51const PL_MAGIC_1200000: i64 = 1200000 52const PL_MAGIC_600000: i64 = 600000 53const PL_MAGIC_1640000: i64 = 1640000 54const PL_MAGIC_800000: i64 = 800000 55const PL_MAGIC_1760000: i64 = 1760000 56const PL_MAGIC_10000: i64 = 10000 57const PL_MAGIC_8000000: i64 = 8000000 58const PL_MAGIC_2840000: i64 = 2840000 59const PL_MAGIC_2412: i64 = 2412 60const PL_MAGIC_5000: i64 = 5000 61const PL_MAGIC_6000: i64 = 6000 62const PL_MAGIC_500000: i64 = 500000 63const PL_MAGIC_1500: i64 = 1500 64 65const PL_REGBUF: i64 = 65536 66const PL_OUTBUF: i64 = 16384 67const PL_KEYBUF: i64 = 256 68const PL_FLDBUF: i64 = 256 69const PL_NTIER: i64 = 4 70const PL_INFEAS: i64 = 0 - 1 // tier cannot serve this volume (capacity ceiling) 71const PL_NOROW: i64 = 0 - 2 // plane carries no row for this process_tier 72 73func plw(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 } 74 75func pl_eq(a: *u8, b: *u8) -> i64 { 76 var i: i64 = 0 77 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 78 if b[i] != (0 as u8) { return 0 } 79 return 1 80} 81 82func pl_atoi(s: *u8) -> i64 { 83 var v: i64 = 0 84 var i: i64 = 0 85 while s[i] != (0 as u8) { 86 let c: i64 = s[i] as i64 87 if c < 48 { return v } 88 if c > 57 { return v } 89 v = v * 10 + (c - 48) 90 i = i + 1 91 } 92 return v 93} 94 95func pl_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 } 96func pl_num(out: *u8, o: i64, v: i64) -> i64 { 97 var x: i64 = v 98 var p: i64 = o 99 if x < 0 { out[p] = 45 as u8; p = p + 1; x = 0 - x } 100 if x == 0 { out[p] = 48 as u8; return p + 1 } 101 var d: i64 = 0 102 var t: i64 = x 103 while t > 0 { d = d + 1; t = t / 10 } 104 var i: i64 = d - 1 105 while i >= 0 { out[p + i] = ((x % 10) + 48) as u8; x = x / 10; i = i - 1 } 106 return p + d 107} 108 109// ---- plane row lookup: field 0 match, then extract field <idx> ------------------------------ 110func pl_field0_eq(buf: *u8, n: i64, rs: i64, key: *u8) -> i64 { 111 var p: i64 = rs 112 var m: i64 = 0 113 while key[m] != (0 as u8) { 114 if p >= n { return 0 } 115 if buf[p] != key[m] { return 0 } 116 p = p + 1; m = m + 1 117 } 118 if p >= n { return 1 } 119 if buf[p] == (9 as u8) { return 1 } 120 if buf[p] == (10 as u8) { return 1 } 121 return 0 122} 123func pl_extract(buf: *u8, n: i64, rs: i64, idx: i64, out: *u8, outcap: i64) -> i64 { 124 var fp: i64 = rs 125 var cf: i64 = 0 126 var oo: i64 = 0 127 var scan: i64 = 1 128 while scan == 1 { 129 if fp >= n { scan = 0 } else { 130 let ch: i64 = buf[fp] as i64 131 if ch == 10 { scan = 0 } else { 132 if ch == 9 { 133 if cf == idx { scan = 0 } else { cf = cf + 1; fp = fp + 1 } 134 } else { 135 if cf == idx { if oo < outcap - 1 { out[oo] = ch as u8; oo = oo + 1 } } 136 fp = fp + 1 137 } 138 } 139 } 140 } 141 out[oo] = 0 as u8 142 return oo 143} 144func pl_field(buf: *u8, n: i64, key: *u8, idx: i64, out: *u8, outcap: i64) -> i64 { 145 var i: i64 = 0 146 while i < n { 147 if pl_field0_eq(buf, n, i, key) == 1 { 148 pl_extract(buf, n, i, idx, out, outcap) 149 return 1 150 } 151 var adv: i64 = 1 152 while adv == 1 { 153 if i >= n { adv = 0 } else { 154 if buf[i] == (10 as u8) { i = i + 1; adv = 0 } else { i = i + 1 } 155 } 156 } 157 } 158 return 0 159} 160func pl_load(rbuf: *u8) -> i64 { return sts_load("knowledge/store/prodladder-" as *u8, rbuf, PL_REGBUF) } 161 162// build "<process>_<suffix>" into out 163func pl_mkkey(process: *u8, suffix: *u8, out: *u8) -> i64 { 164 var o: i64 = 0 165 var i: i64 = 0 166 while process[i] != (0 as u8) { if o < PL_KEYBUF - 8 { out[o] = process[i]; o = o + 1 } i = i + 1 } 167 var j: i64 = 0 168 while suffix[j] != (0 as u8) { if o < PL_KEYBUF - 2 { out[o] = suffix[j]; o = o + 1 } j = j + 1 } 169 out[o] = 0 as u8 170 return o 171} 172 173func pl_tier_suffix(t: i64) -> *u8 { 174 if t == 0 { return "_t0" as *u8 } 175 if t == 1 { return "_t1" as *u8 } 176 if t == 2 { return "_t2" as *u8 } 177 return "_t3" as *u8 178} 179 180// ---- THE CORE ARITHMETIC (pure; what the selftest proves) ----------------------------------- 181// 182// Standing cost = the part that does NOT move with volume: amortised one-time + fixed annual. 183// Amortisation is integer division by life_years. life<=0 is treated as 1 rather than dividing by 184// zero -- a malformed row must not crash a pricing call, but it must not silently price as free 185// either, so the FULL one-time cost lands in year one, which is the conservative direction. 186func pl_standing(capex_c: i64, tooling_c: i64, fixed_annual_c: i64, life_years: i64) -> i64 { 187 var life: i64 = life_years 188 if life <= 0 { life = 1 } 189 return ((capex_c + tooling_c) / life) + fixed_annual_c 190} 191 192// Total annual cost to make `vol` units on this tier. PL_INFEAS when the tier cannot physically 193// carry the volume -- a capacity ceiling is a HARD wall, not a cost penalty, and pricing past it 194// would recommend a machine that cannot make the parts. 195func pl_annual(capex_c: i64, tooling_c: i64, fixed_annual_c: i64, per_unit_c: i64, 196 life_years: i64, max_units: i64, vol: i64) -> i64 { 197 if max_units > 0 { if vol > max_units { return PL_INFEAS } } 198 return pl_standing(capex_c, tooling_c, fixed_annual_c, life_years) + (per_unit_c * vol) 199} 200 201// Crossover volume between a CHEAP-TO-START tier A (high per-unit) and a CHEAP-TO-RUN tier B 202// (high standing). Returns the first integer volume at which B is <= A -- ceiling division, so the 203// answer is the volume where you should ACTUALLY step up, not the one just before it. 204// Returns 0 when B never wins (B is not cheaper per unit) -- honest "no crossover", not a 205// fabricated huge number. 206func pl_breakeven(standingA: i64, per_unitA: i64, standingB: i64, per_unitB: i64) -> i64 { 207 if per_unitB >= per_unitA { return 0 } 208 if standingB <= standingA { return 0 } 209 let dstand: i64 = standingB - standingA 210 let dunit: i64 = per_unitA - per_unitB 211 var v: i64 = dstand / dunit 212 if (dstand % dunit) != 0 { v = v + 1 } 213 return v 214} 215 216// ---- row load: pulls one tier's economics out of the plane into box[0..6] -------------------- 217// box: 0 capex 1 tooling 2 fixed 3 per_unit 4 life 5 max_units 6 present(1/0) 218func pl_row(rbuf: *u8, rn: i64, process: *u8, tier: i64, box: *i64, name: *u8, namecap: i64) -> i64 { 219 let key: *u8 = sys_mmap(PL_KEYBUF) 220 pl_mkkey(process, pl_tier_suffix(tier), key) 221 let tmp: *u8 = sys_mmap(PL_FLDBUF) 222 if pl_field(rbuf, rn, key, 1, name, namecap) == 0 { box[6] = 0; return PL_NOROW } 223 pl_field(rbuf, rn, key, 2, tmp, PL_FLDBUF); box[0] = pl_atoi(tmp) 224 pl_field(rbuf, rn, key, 3, tmp, PL_FLDBUF); box[1] = pl_atoi(tmp) 225 pl_field(rbuf, rn, key, 4, tmp, PL_FLDBUF); box[2] = pl_atoi(tmp) 226 pl_field(rbuf, rn, key, 5, tmp, PL_FLDBUF); box[3] = pl_atoi(tmp) 227 pl_field(rbuf, rn, key, 6, tmp, PL_FLDBUF); box[4] = pl_atoi(tmp) 228 pl_field(rbuf, rn, key, 7, tmp, PL_FLDBUF); box[5] = pl_atoi(tmp) 229 box[6] = 1 230 return 1 231} 232 233// ---- verb: calc ------------------------------------------------------------------------------ 234func pl_calc(process: *u8, vol: i64) -> i64 { 235 let out: *u8 = sys_mmap(PL_OUTBUF) 236 if vol <= 0 { 237 plw(2, "nx_prodladder: annual_units must be > 0\n" as *u8) 238 return 2 239 } 240 let rbuf: *u8 = sys_mmap(PL_REGBUF) 241 let rn: i64 = pl_load(rbuf) 242 243 let box: *i64 = sys_mmap(64) as *i64 244 let name: *u8 = sys_mmap(PL_FLDBUF) 245 246 var found: i64 = 0 247 var best_t: i64 = 0 - 1 248 var best_c: i64 = 0 249 var o: i64 = 0 250 o = pl_lit(out, o, "{\"v\":1,\"organ\":\"nx_prodladder\",\"verb\":\"calc\",\"process\":\"" as *u8) 251 o = pl_lit(out, o, process) 252 o = pl_lit(out, o, "\",\"annual_units\":" as *u8) 253 o = pl_num(out, o, vol) 254 o = pl_lit(out, o, ",\"tiers\":[" as *u8) 255 256 var t: i64 = 0 257 var emitted: i64 = 0 258 while t < PL_NTIER { 259 if pl_row(rbuf, rn, process, t, box, name, PL_FLDBUF) == 1 { 260 found = found + 1 261 let ann: i64 = pl_annual(box[0], box[1], box[2], box[3], box[4], box[5], vol) 262 if emitted > 0 { o = pl_lit(out, o, "," as *u8) } 263 o = pl_lit(out, o, "{\"tier\":" as *u8) 264 o = pl_num(out, o, t) 265 o = pl_lit(out, o, ",\"name\":\"" as *u8) 266 o = pl_lit(out, o, name) 267 o = pl_lit(out, o, "\",\"standing_c\":" as *u8) 268 o = pl_num(out, o, pl_standing(box[0], box[1], box[2], box[4])) 269 o = pl_lit(out, o, ",\"per_unit_c\":" as *u8) 270 o = pl_num(out, o, box[3]) 271 if ann == PL_INFEAS { 272 o = pl_lit(out, o, ",\"annual_c\":null,\"unit_cost_c\":null,\"feasible\":0,\"why\":\"volume exceeds tier capacity ceiling\"}" as *u8) 273 } else { 274 o = pl_lit(out, o, ",\"annual_c\":" as *u8) 275 o = pl_num(out, o, ann) 276 o = pl_lit(out, o, ",\"unit_cost_c\":" as *u8) 277 o = pl_num(out, o, ann / vol) 278 o = pl_lit(out, o, ",\"feasible\":1}" as *u8) 279 if best_t < 0 { best_t = t; best_c = ann } else { if ann < best_c { best_t = t; best_c = ann } } 280 } 281 emitted = emitted + 1 282 } 283 t = t + 1 284 } 285 o = pl_lit(out, o, "]" as *u8) 286 287 if found == 0 { 288 // REFUSE. See the header: a ladder that invents its own capex is worse than no ladder. 289 plw(2, "nx_prodladder: REFUSED -- knowledge/store/prodladder- carries no rows for process '" as *u8) 290 plw(2, process) 291 plw(2, "'.\n This organ does NOT substitute a plausible default: a fabricated break-even volume gets spent against.\n Seed real quoted economics first: nx_store_put knowledge/store/prodladder- put <actor> <process>_t0 <name> <capex_c> <tooling_c> <fixed_annual_c> <per_unit_c> <life_years> <max_annual_units> <note>\n" as *u8) 292 return 4 293 } 294 295 o = pl_lit(out, o, ",\"chosen_tier\":" as *u8) 296 o = pl_num(out, o, best_t) 297 o = pl_lit(out, o, ",\"chosen_annual_c\":" as *u8) 298 o = pl_num(out, o, best_c) 299 o = pl_lit(out, o, ",\"chosen_unit_cost_c\":" as *u8) 300 o = pl_num(out, o, best_c / vol) 301 o = pl_lit(out, o, ",\"tiers_in_plane\":" as *u8) 302 o = pl_num(out, o, found) 303 o = pl_lit(out, o, ",\"grounding\":\"plane knowledge/store/prodladder-; integer-exact cents; no defaults substituted\"}\n" as *u8) 304 sys_write(1, out, o) 305 return 0 306} 307 308// ---- verb: ramp -- the LADDER across a volume ramp ------------------------------------------- 309func pl_ramp(process: *u8, vols: *i64, nv: i64) -> i64 { 310 let out: *u8 = sys_mmap(PL_OUTBUF) 311 let rbuf: *u8 = sys_mmap(PL_REGBUF) 312 let rn: i64 = pl_load(rbuf) 313 let box: *i64 = sys_mmap(64) as *i64 314 let name: *u8 = sys_mmap(PL_FLDBUF) 315 316 var any: i64 = 0 317 var t0: i64 = 0 318 while t0 < PL_NTIER { if pl_row(rbuf, rn, process, t0, box, name, PL_FLDBUF) == 1 { any = any + 1 } t0 = t0 + 1 } 319 if any == 0 { 320 plw(2, "nx_prodladder: REFUSED -- no plane rows for process '" as *u8) 321 plw(2, process) 322 plw(2, "' (see `calc` for the seed recipe)\n" as *u8) 323 return 4 324 } 325 326 var o: i64 = 0 327 o = pl_lit(out, o, "{\"v\":1,\"organ\":\"nx_prodladder\",\"verb\":\"ramp\",\"process\":\"" as *u8) 328 o = pl_lit(out, o, process) 329 o = pl_lit(out, o, "\",\"steps\":[" as *u8) 330 var i: i64 = 0 331 var prev_t: i64 = 0 - 1 332 while i < nv { 333 let v: i64 = vols[i] 334 var bt: i64 = 0 - 1 335 var bc: i64 = 0 336 var bn: *u8 = "" as *u8 337 var t: i64 = 0 338 while t < PL_NTIER { 339 if pl_row(rbuf, rn, process, t, box, name, PL_FLDBUF) == 1 { 340 let ann: i64 = pl_annual(box[0], box[1], box[2], box[3], box[4], box[5], v) 341 if ann != PL_INFEAS { 342 if bt < 0 { bt = t; bc = ann; bn = name } else { if ann < bc { bt = t; bc = ann; bn = name } } 343 } 344 } 345 t = t + 1 346 } 347 if i > 0 { o = pl_lit(out, o, "," as *u8) } 348 o = pl_lit(out, o, "{\"annual_units\":" as *u8) 349 o = pl_num(out, o, v) 350 if bt < 0 { 351 o = pl_lit(out, o, ",\"tier\":null,\"why\":\"no tier in the plane can carry this volume\"}" as *u8) 352 } else { 353 o = pl_lit(out, o, ",\"tier\":" as *u8) 354 o = pl_num(out, o, bt) 355 o = pl_lit(out, o, ",\"annual_c\":" as *u8) 356 o = pl_num(out, o, bc) 357 o = pl_lit(out, o, ",\"unit_cost_c\":" as *u8) 358 o = pl_num(out, o, bc / v) 359 var stepped: i64 = 0 360 if prev_t >= 0 { if bt != prev_t { stepped = 1 } } 361 o = pl_lit(out, o, ",\"step_up\":" as *u8) 362 o = pl_num(out, o, stepped) 363 o = pl_lit(out, o, "}" as *u8) 364 prev_t = bt 365 } 366 i = i + 1 367 } 368 o = pl_lit(out, o, "],\"crossovers\":[" as *u8) 369 370 // Adjacent-tier crossovers: the actual "stop renting, start buying" volumes. 371 var a: i64 = 0 372 var ce: i64 = 0 373 while a < PL_NTIER - 1 { 374 let boxA: *i64 = sys_mmap(64) as *i64 375 let nA: *u8 = sys_mmap(PL_FLDBUF) 376 if pl_row(rbuf, rn, process, a, boxA, nA, PL_FLDBUF) == 1 { 377 var b: i64 = a + 1 378 while b < PL_NTIER { 379 let boxB: *i64 = sys_mmap(64) as *i64 380 let nB: *u8 = sys_mmap(PL_FLDBUF) 381 if pl_row(rbuf, rn, process, b, boxB, nB, PL_FLDBUF) == 1 { 382 let sA: i64 = pl_standing(boxA[0], boxA[1], boxA[2], boxA[4]) 383 let sB: i64 = pl_standing(boxB[0], boxB[1], boxB[2], boxB[4]) 384 let be: i64 = pl_breakeven(sA, boxA[3], sB, boxB[3]) 385 if be > 0 { 386 if ce > 0 { o = pl_lit(out, o, "," as *u8) } 387 o = pl_lit(out, o, "{\"from_tier\":" as *u8) 388 o = pl_num(out, o, a) 389 o = pl_lit(out, o, ",\"to_tier\":" as *u8) 390 o = pl_num(out, o, b) 391 o = pl_lit(out, o, ",\"breakeven_annual_units\":" as *u8) 392 o = pl_num(out, o, be) 393 o = pl_lit(out, o, "}" as *u8) 394 ce = ce + 1 395 } 396 b = PL_NTIER 397 } else { b = b + 1 } 398 } 399 } 400 a = a + 1 401 } 402 o = pl_lit(out, o, "]}\n" as *u8) 403 sys_write(1, out, o) 404 return 0 405} 406 407// ---- selftest: proves the ARITHMETIC on SYNTHETIC fixtures + the REFUSAL on a missing process -- 408// These numbers are FIXTURES, not market data. They exist to prove the transform is exact and the 409// crossover lands on the right integer; they assert nothing about what a real machine costs. 410func pl_chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 411 st[0] = st[0] + 1 412 if got == want { st[1] = st[1] + 1; return 1 } 413 plw(2, " FAIL " as *u8); plw(2, name) 414 let b: *u8 = sys_mmap(64) 415 var o: i64 = 0 416 o = pl_lit(b, o, " got=" as *u8); o = pl_num(b, o, got) 417 o = pl_lit(b, o, " want=" as *u8); o = pl_num(b, o, want) 418 o = pl_lit(b, o, "\n" as *u8) 419 sys_write(2, b, o) 420 return 0 421} 422 423func pl_selftest() -> i64 { 424 let st: *i64 = sys_mmap(32) as *i64 425 st[0] = 0 426 st[1] = 0 427 428 // FIXTURE A -- rent/service bureau: no capex, no tooling, no standing cost, $8.00/unit. 429 // FIXTURE B -- buy: $40,000 machine + $12,000 tooling over 5 years, $6,000/yr standing, $1.20/unit. 430 let a_cap: i64 = 0 431 let a_tool: i64 = 0 432 let a_fix: i64 = 0 433 let a_unit: i64 = 800 434 let a_life: i64 = 1 435 let b_cap: i64 = PL_MAGIC_4000000 436 let b_tool: i64 = PL_MAGIC_1200000 437 let b_fix: i64 = PL_MAGIC_600000 438 let b_unit: i64 = 120 439 let b_life: i64 = 5 440 441 pl_chk("T1 standing(rent)=0" as *u8, pl_standing(a_cap, a_tool, a_fix, a_life), 0, st) 442 // (4,000,000 + 1,200,000)/5 + 600,000 = 1,040,000 + 600,000 443 pl_chk("T2 standing(buy)=1640000" as *u8, pl_standing(b_cap, b_tool, b_fix, b_life), PL_MAGIC_1640000, st) 444 445 // At 1,000 units renting wins: 800,000 vs 1,760,000. 446 pl_chk("T3 rent@1k=800000" as *u8, pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,0,1000), PL_MAGIC_800000, st) 447 pl_chk("T4 buy@1k=1760000" as *u8, pl_annual(b_cap,b_tool,b_fix,b_unit,b_life,0,1000), PL_MAGIC_1760000, st) 448 449 // At 10,000 units buying wins: 8,000,000 vs 2,840,000. The ladder must INVERT across volume -- 450 // if it never inverts the whole organ is decoration. 451 pl_chk("T5 rent@10k=8000000" as *u8, pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,0,PL_MAGIC_10000), PL_MAGIC_8000000, st) 452 pl_chk("T6 buy@10k=2840000" as *u8, pl_annual(b_cap,b_tool,b_fix,b_unit,b_life,0,PL_MAGIC_10000), PL_MAGIC_2840000, st) 453 454 // Crossover: 1,640,000 / (800-120) = 2411.76 -> 2412 (ceiling: the first volume where buying wins). 455 let be: i64 = pl_breakeven(0, a_unit, PL_MAGIC_1640000, b_unit) 456 pl_chk("T7 breakeven=2412" as *u8, be, PL_MAGIC_2412, st) 457 458 // PROVEN BOTH WAYS -- the crossover is only real if the cheaper tier actually flips there. 459 let r_at: i64 = pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,0,be) 460 let b_at: i64 = pl_annual(b_cap,b_tool,b_fix,b_unit,b_life,0,be) 461 let r_bef: i64 = pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,0,be - 1) 462 let b_bef: i64 = pl_annual(b_cap,b_tool,b_fix,b_unit,b_life,0,be - 1) 463 var at_ok: i64 = 0 464 if b_at <= r_at { at_ok = 1 } 465 var bef_ok: i64 = 0 466 if r_bef < b_bef { bef_ok = 1 } 467 pl_chk("T8 at breakeven buy<=rent" as *u8, at_ok, 1, st) 468 pl_chk("T9 one unit earlier rent<buy" as *u8, bef_ok, 1, st) 469 470 // Capacity ceiling is a HARD wall, not a cost penalty. 471 pl_chk("T10 over-ceiling=INFEASIBLE" as *u8, pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,PL_MAGIC_5000,PL_MAGIC_6000), PL_INFEAS, st) 472 pl_chk("T11 at-ceiling feasible" as *u8, pl_annual(a_cap,a_tool,a_fix,a_unit,a_life,PL_MAGIC_5000,PL_MAGIC_5000), PL_MAGIC_4000000, st) 473 474 // No crossover when the "cheap to run" tier is not actually cheaper per unit -- must answer 0, 475 // never a fabricated large volume. 476 pl_chk("T12 no-crossover=0" as *u8, pl_breakeven(0, 100, PL_MAGIC_500000, 200), 0, st) 477 478 // A malformed life=0 row must not divide by zero; the whole one-time cost lands in year one. 479 pl_chk("T13 life0 conservative" as *u8, pl_standing(1000, 500, 0, 0), PL_MAGIC_1500, st) 480 481 // REFUSAL: a process the plane does not carry must exit nonzero, not price at a default. 482 let rbuf: *u8 = sys_mmap(PL_REGBUF) 483 let rn: i64 = pl_load(rbuf) 484 let box: *i64 = sys_mmap(64) as *i64 485 let nm: *u8 = sys_mmap(PL_FLDBUF) 486 let miss: i64 = pl_row(rbuf, rn, "__nx_absent_process__" as *u8, 0, box, nm, PL_FLDBUF) 487 pl_chk("T14 absent process = NOROW" as *u8, miss, PL_NOROW, st) 488 pl_chk("T15 absent process not present" as *u8, box[6], 0, st) 489 490 let b2: *u8 = sys_mmap(256) 491 var o: i64 = 0 492 o = pl_lit(b2, o, "SELFTEST " as *u8) 493 o = pl_num(b2, o, st[1]) 494 o = pl_lit(b2, o, "/" as *u8) 495 o = pl_num(b2, o, st[0]) 496 if st[1] == st[0] { 497 o = pl_lit(b2, o, " VERDICT=GREEN (integer-exact; ladder INVERTS across volume, crossover proven BOTH WAYS, capacity ceiling hard, refusal fail-closed)\n" as *u8) 498 sys_write(1, b2, o) 499 return 0 500 } 501 o = pl_lit(b2, o, " VERDICT=RED\n" as *u8) 502 sys_write(1, b2, o) 503 return 1 504} 505 506func main(argc: i64, argv: *i64) -> i64 { 507 if argc < 2 { 508 plw(1, "usage: nx_prodladder calc <process> <annual_units> | ramp <process> <v1> [v2..v6] | selftest\n" as *u8) 509 plw(1, " tier economics live in the seg-store plane knowledge/store/prodladder- (one row per <process>_t0.._t3)\n" as *u8) 510 plw(1, " NO row -> REFUSE. This organ never substitutes a default capex.\n" as *u8) 511 return 2 512 } 513 let verb: *u8 = argv[1] as *u8 514 if pl_eq(verb, "selftest" as *u8) == 1 { return pl_selftest() } 515 if pl_eq(verb, "calc" as *u8) == 1 { 516 if argc < 4 { plw(2, "usage: nx_prodladder calc <process> <annual_units>\n" as *u8); return 2 } 517 return pl_calc(argv[2] as *u8, pl_atoi(argv[3] as *u8)) 518 } 519 if pl_eq(verb, "ramp" as *u8) == 1 { 520 if argc < 4 { plw(2, "usage: nx_prodladder ramp <process> <v1> [v2..v6]\n" as *u8); return 2 } 521 let vols: *i64 = sys_mmap(64) as *i64 522 var nv: i64 = 0 523 var i: i64 = 3 524 while i < argc { 525 if nv < 6 { vols[nv] = pl_atoi(argv[i] as *u8); nv = nv + 1 } 526 i = i + 1 527 } 528 return pl_ramp(argv[2] as *u8, vols, nv) 529 } 530 plw(2, "nx_prodladder: unknown verb (want calc | ramp | selftest)\n" as *u8) 531 return 2 532}