code wiki / _hdl_build / nx_flip_score.nx

nx_flip_score.nx source

↩ module page · 405 lines · 19228 B

1// nx_flip_score.nx -- THE ASSET-AGNOSTIC COMPONENT-LEVEL FLIP ENGINE. 2// Operator: "an engine not me typing every part" -- reads the part list ITSELF from the planes and 3// CHOOSES the repair set component-by-component. Same math for a car, a house, any asset with 4// value-bearing components (class/component/finding are DATA, not code). 5// PER-COMPONENT (all downside-priced): margin_typ=lift_typ-repair_typ ; margin_p10=lift_low-repair_high 6// MANDATORY (cost forced) / FLIPPOINT (margin_p10>=0) / SPECULATIVE (class-gated) / LEAVE. 7// COVERAGE GATE: every MANDATORY catalog component for the class needs a finding, else REFUSED. 8// DEAL GATE: BUY iff profit_p10>=gate_min AND days<=dts_cap. Every threshold from flip-class-. 9// 10// CONVERTED ONTO THE SHARED BASE nx_flip_lib.nx (fx_*); the ~17 private helpers are GONE. Behaviour 11// PROVEN identical by nx_flip_gate staying 16/16 across the swap. 12// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 13import "nx_flip_lib.nx" 14import "nx_hazard_lib.nx" 15import "nx_syscalls.nx" 16 17const FL_ARGC_MIN: i64 = 10 18const FL_ARG_PFX: i64 = 10 19const FL_CC_GATE: i64 = 2 20const FL_CC_SPEC: i64 = 3 21const FL_CC_DTS: i64 = 4 22const FL_CC_SELLBPS: i64 = 5 23const FL_CC_HOLD: i64 = 6 24const FL_CK_CLASS: i64 = 1 25const FL_CK_MAND: i64 = 3 26const FL_CF_DEAL: i64 = 1 27const FL_CF_COMP: i64 = 2 28const FL_CF_COND: i64 = 3 29const FL_CF_REPLOW: i64 = 4 30const FL_CF_REPTYP: i64 = 5 31const FL_CF_REPHIGH: i64 = 6 32const FL_CF_LIFTLOW: i64 = 7 33const FL_CF_LIFTTYP: i64 = 8 34const FL_A_LEAVE: i64 = 0 35const FL_A_MAND: i64 = 1 36const FL_A_FLIP: i64 = 2 37const FL_A_SPEC: i64 = 3 38// ---- WEAR EXPOSURE (added 2026-08-08), OPT-IN AT ARGV so existing callers are byte-identical. 39// The defect it closes, measured on this organ's own live output: for every part the engine 40// declines to fix, `margin = lift - repair` prices the RISK OF IT FAILING DURING THE HOLD at 41// ZERO. On the real DPS6 curve (beta=1.7274 eta=111823, fitted from 27 NHTSA failure mileages) 42// that unpriced risk is 29%-52% of the entire p10 downside margin from ONE component. 43// usage_now<=0 => hazard OFF and every number is exactly what it was: that is what keeps 44// nx_flip_gate at 23/23 across this change rather than making 23 assertions a negotiation. 45const FL_ARG_USAGE: i64 = 11 46const FL_ARG_HOLD: i64 = 12 47const FL_OBS_CLASS: i64 = 1 48const FL_OBS_COMP: i64 = 2 49const FL_OBS_USAGE: i64 = 3 50const FL_CFG_MINOBS: i64 = 1 51const FL_CFG_MINR2: i64 = 2 52const FL_MAXOBS: i64 = 4096 53const FL_HZSLOTS: i64 = 8 54 55// Expected cost of THIS component failing during the hold, from the observations plane. 56// Returns the money figure; sets unfit[0]=1 when no curve could be fitted for this component. 57// 58// UNFITTABLE IS REPORTED, NOT REFUSED -- and this is the one deliberate fail-OPEN in the family. 59// Refusing a whole deal because one part lacks failure data would make the engine unusable 60// (measured: only 1 of 14 components on the 2013 Focus has enough NHTSA mileage to fit at all), 61// and silently returning 0 is the very defect being fixed. So the exposure is 0 AND the caller 62// is handed a COUNT of unpriced components to print. Declare the coverage; never imply it. 63// 64// DUPLICATION DECLARED: nx_flip_hazard carries the same gather+fit inline. Rule 15 extracts at 65// three uses; this is the second. Filed against myself to be extracted into nx_flip_lib on the 66// next touch rather than left for someone else to rediscover. 67func fl_exposure(bo: *u8, no: i64, min_obs: i64, min_r2: i64, cls: *u8, 68 kb: *u8, k0: i64, k1: i64, usage: i64, hold: i64, rep_high: i64, 69 sp: *i64, unfit: *i64) -> i64 { 70 unfit[0] = 0 71 if usage <= 0 { return 0 } 72 if no == 0 { unfit[0] = 1; return 0 } 73 let ts: *i64 = sts_mm(FL_MAXOBS * HZ_I64) as *i64 74 var n: i64 = 0 75 var i: i64 = 0 76 while i < no { 77 let le: i64 = fx_line_end(bo, no, i) 78 if le > i { 79 if fx_col(bo, i, le, FL_OBS_CLASS, sp) == 1 { 80 if fx_slice_eqs(bo, sp[0], sp[1], cls) == 1 { 81 if fx_col(bo, i, le, FL_OBS_COMP, sp) == 1 { 82 if fx_slice_eq(bo, sp[0], sp[1], kb, k0, k1) == 1 { 83 if n < FL_MAXOBS { 84 ts[n] = fx_col_atoi(bo, i, le, FL_OBS_USAGE, sp) 85 n = n + 1 86 } 87 } 88 } 89 } 90 } 91 } 92 i = le + 1 93 } 94 if n == 0 { unfit[0] = 1; return 0 } 95 let hout: *i64 = sts_mm(FL_HZSLOTS * HZ_I64) as *i64 96 if hz_wb_fit(ts, n, min_obs, min_r2, hout) == 0 { unfit[0] = 1; return 0 } 97 let pf: i64 = hz_cond_pfail_bps(usage, usage + hold, hout[0], hout[1]) 98 if pf < 0 { unfit[0] = 1; return 0 } 99 // rep_high, not rep_typ: this feeds the P10 gate, which is the DOWNSIDE gate. 100 return hz_expected_cost(pf, rep_high) 101} 102 103func main(argc: i64, argv: *i64) -> i64 { 104 if argc < FL_ARGC_MIN { 105 fx_puts("usage: nx_flip_score <class> <deal> <asking> <resale_typ> <resale_low> <recon> <transport> <fees> <days> [plane-prefix-root]\n" as *u8) 106 fx_puts(" parts are NOT typed -- they are read from <root>comp- (catalog) + <root>find- (findings)\n" as *u8) 107 sys_exit(FX_EXIT_USAGE) 108 return FX_EXIT_USAGE 109 } 110 let cls: *u8 = argv[1] as *u8 111 let deal: *u8 = argv[2] as *u8 112 let asking: i64 = fx_atoi(argv[3] as *u8) 113 let resale_typ: i64 = fx_atoi(argv[4] as *u8) 114 let resale_low: i64 = fx_atoi(argv[5] as *u8) 115 let recon: i64 = fx_atoi(argv[6] as *u8) 116 let transport: i64 = fx_atoi(argv[7] as *u8) 117 let fees: i64 = fx_atoi(argv[8] as *u8) 118 let days: i64 = fx_atoi(argv[9] as *u8) 119 120 var root: *u8 = "flip-" as *u8 121 if argc > FL_ARG_PFX { root = argv[FL_ARG_PFX] as *u8 } 122 let ncp: *i64 = sts_mm(FX_SCRATCH) as *i64 123 let nkp: *i64 = sts_mm(FX_SCRATCH) as *i64 124 let nfp: *i64 = sts_mm(FX_SCRATCH) as *i64 125 let bc: *u8 = fx_plane(root, "class-" as *u8, ncp) 126 let bk: *u8 = fx_plane(root, "comp-" as *u8, nkp) 127 let bf: *u8 = fx_plane(root, "find-" as *u8, nfp) 128 let nc: i64 = ncp[0] 129 let nk: i64 = nkp[0] 130 let nf: i64 = nfp[0] 131 132 // OPT-IN wear inputs. Absent => usage_now 0 => hazard OFF => identical output. 133 var usage_now: i64 = 0 134 var hz_hold: i64 = 0 135 if argc > FL_ARG_USAGE { usage_now = fx_atoi(argv[FL_ARG_USAGE] as *u8) } 136 if argc > FL_ARG_HOLD { hz_hold = fx_atoi(argv[FL_ARG_HOLD] as *u8) } 137 let nobp: *i64 = sts_mm(FX_SCRATCH) as *i64 138 let nhcp: *i64 = sts_mm(FX_SCRATCH) as *i64 139 var bo: *u8 = fx_plane(root, "obs-" as *u8, nobp) 140 let bhc: *u8 = fx_plane(root, "hzcfg-" as *u8, nhcp) 141 var n_obs_plane: i64 = nobp[0] 142 if usage_now <= 0 { n_obs_plane = 0 } 143 let unfit: *i64 = sts_mm(FX_SCRATCH) as *i64 144 var hz_min_obs: i64 = 0 145 var hz_min_r2: i64 = 0 146 var exposure: i64 = 0 147 var n_unpriced: i64 = 0 148 var n_priced: i64 = 0 149 150 let sp: *i64 = sts_mm(FX_SCRATCH) as *i64 151 let cr: *i64 = sts_mm(FX_SCRATCH) as *i64 152 let kr: *i64 = sts_mm(FX_SCRATCH) as *i64 153 let cs: *i64 = sts_mm(FX_SCRATCH) as *i64 154 155 if nc == 0 { 156 fx_puts("FLIP-RED reason=class-plane-empty prefix=" as *u8); fx_puts(fx_cat2(root, "class-" as *u8)); fx_puts("\n" as *u8) 157 sys_exit(FX_EXIT_REFUSED) 158 return FX_EXIT_REFUSED 159 } 160 if nk == 0 { 161 fx_puts("FLIP-RED reason=component-plane-empty prefix=" as *u8); fx_puts(fx_cat2(root, "comp-" as *u8)); fx_puts("\n" as *u8) 162 sys_exit(FX_EXIT_REFUSED) 163 return FX_EXIT_REFUSED 164 } 165 if fx_row_by_id(bc, nc, cls, cr, sp) == 0 { 166 fx_puts("FLIP-RED reason=unknown-asset-class class=" as *u8); fx_puts(cls); fx_puts("\n" as *u8) 167 sys_exit(FX_EXIT_REFUSED) 168 return FX_EXIT_REFUSED 169 } 170 let gate_min: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_GATE, sp) 171 let take_spec: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_SPEC, sp) 172 let dts_cap: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_DTS, sp) 173 let sell_bps: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_SELLBPS, sp) 174 let hold_day: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_HOLD, sp) 175 // Hazard thresholds come from the plane, never from this code (rule 11). If the class has 176 // no hzcfg row the exposure pass stays OFF rather than inventing a bar. 177 let hcr: *i64 = sts_mm(FX_SCRATCH) as *i64 178 if n_obs_plane > 0 { 179 if fx_row_by_id(bhc, nhcp[0], cls, hcr, sp) == 1 { 180 hz_min_obs = fx_col_atoi(bhc, hcr[0], hcr[1], FL_CFG_MINOBS, sp) 181 hz_min_r2 = fx_col_atoi(bhc, hcr[0], hcr[1], FL_CFG_MINR2, sp) 182 } else { n_obs_plane = 0 } 183 } 184 185 let pb: *u8 = sts_mm(FX_BUF) 186 var po: i64 = 0 187 var rep_t: i64 = 0 188 var rep_h: i64 = 0 189 var lift_t: i64 = 0 190 var lift_l: i64 = 0 191 var mand_h: i64 = 0 192 var flip_gain: i64 = 0 193 var n_mand: i64 = 0 194 var n_flip: i64 = 0 195 var n_spec: i64 = 0 196 var n_leave: i64 = 0 197 var n_seen: i64 = 0 198 var n_unk: i64 = 0 199 200 var i: i64 = 0 201 while i < nf { 202 let le: i64 = fx_line_end(bf, nf, i) 203 if le > i { 204 if fx_col(bf, i, le, FL_CF_DEAL, sp) == 1 { 205 if fx_slice_eqs(bf, sp[0], sp[1], deal) == 1 { 206 n_seen = n_seen + 1 207 fx_col(bf, i, le, FL_CF_COMP, cs) 208 let cond: i64 = fx_col_atoi(bf, i, le, FL_CF_COND, sp) 209 let rt: i64 = fx_col_atoi(bf, i, le, FL_CF_REPTYP, sp) 210 let rh: i64 = fx_col_atoi(bf, i, le, FL_CF_REPHIGH, sp) 211 let ll: i64 = fx_col_atoi(bf, i, le, FL_CF_LIFTLOW, sp) 212 let lt: i64 = fx_col_atoi(bf, i, le, FL_CF_LIFTTYP, sp) 213 214 var mand: i64 = 1 215 var known: i64 = 0 216 if fx_row_by_slice(bk, nk, bf, cs[0], cs[1], kr, sp) == 1 { 217 known = 1 218 mand = fx_col_atoi(bk, kr[0], kr[1], FL_CK_MAND, sp) 219 } 220 let m_t: i64 = lt - rt 221 let m_p: i64 = ll - rh 222 var act: i64 = FL_A_LEAVE 223 var take: i64 = 0 224 if mand == 1 { 225 act = FL_A_MAND 226 take = 1 227 } else { 228 if m_p >= 0 { 229 act = FL_A_FLIP 230 take = 1 231 } else { 232 if m_t > 0 { act = FL_A_SPEC; take = take_spec } else { act = FL_A_LEAVE; take = 0 } 233 } 234 } 235 if take == 1 { 236 rep_t = rep_t + rt 237 rep_h = rep_h + rh 238 lift_t = lift_t + lt 239 lift_l = lift_l + ll 240 } 241 // THE ASYMMETRY THAT WAS MISSING: a part we DO repair has its failure risk 242 // removed by the repair; a part we DECLINE carries that risk for the whole 243 // hold, and until now that cost was exactly zero in the arithmetic. 244 var pexp: i64 = 0 245 if take == 0 { 246 if n_obs_plane > 0 { 247 pexp = fl_exposure(bo, n_obs_plane, hz_min_obs, hz_min_r2, cls, 248 bf, cs[0], cs[1], usage_now, hz_hold, rh, sp, unfit) 249 if unfit[0] == 1 { n_unpriced = n_unpriced + 1 } else { n_priced = n_priced + 1 } 250 exposure = exposure + pexp 251 } 252 } 253 if act == FL_A_MAND { n_mand = n_mand + 1; mand_h = mand_h + rh } 254 if act == FL_A_FLIP { n_flip = n_flip + 1; flip_gain = flip_gain + m_p } 255 if act == FL_A_SPEC { n_spec = n_spec + 1 } 256 if act == FL_A_LEAVE { n_leave = n_leave + 1 } 257 if known == 0 { n_unk = n_unk + 1 } 258 259 po = fx_bcat(pb, po, "PART " as *u8) 260 po = fx_bcatsl(pb, po, bf, cs[0], cs[1]) 261 po = fx_bcat(pb, po, " cond=" as *u8); po = fx_bcatn(pb, po, cond) 262 po = fx_bcat(pb, po, " rep_typ=" as *u8); po = fx_bcatn(pb, po, rt) 263 po = fx_bcat(pb, po, " rep_high=" as *u8); po = fx_bcatn(pb, po, rh) 264 po = fx_bcat(pb, po, " lift_typ=" as *u8); po = fx_bcatn(pb, po, lt) 265 po = fx_bcat(pb, po, " lift_low=" as *u8); po = fx_bcatn(pb, po, ll) 266 po = fx_bcat(pb, po, " margin_typ=" as *u8); po = fx_bcatn(pb, po, m_t) 267 po = fx_bcat(pb, po, " margin_p10=" as *u8); po = fx_bcatn(pb, po, m_p) 268 if n_obs_plane > 0 { 269 if take == 0 { 270 po = fx_bcat(pb, po, " wear_exposure=" as *u8); po = fx_bcatn(pb, po, pexp) 271 if unfit[0] == 1 { po = fx_bcat(pb, po, " UNPRICED-no-failure-data" as *u8) } 272 } 273 } 274 po = fx_bcat(pb, po, " action=" as *u8) 275 if act == FL_A_MAND { po = fx_bcat(pb, po, "MANDATORY" as *u8) } 276 if act == FL_A_FLIP { po = fx_bcat(pb, po, "FLIPPOINT" as *u8) } 277 if act == FL_A_SPEC { 278 if take == 1 { po = fx_bcat(pb, po, "SPECULATIVE-TAKE" as *u8) } else { po = fx_bcat(pb, po, "SPECULATIVE-SKIP" as *u8) } 279 } 280 if act == FL_A_LEAVE { po = fx_bcat(pb, po, "LEAVE" as *u8) } 281 if known == 0 { po = fx_bcat(pb, po, " WARN=uncatalogued-component-forced-mandatory" as *u8) } 282 po = fx_bcat(pb, po, "\n" as *u8) 283 } 284 } 285 } 286 i = le + 1 287 } 288 289 // COVERAGE GATE: every MANDATORY catalog component for this class needs a finding. 290 var n_missing: i64 = 0 291 let cs2: *i64 = sts_mm(FX_SCRATCH) as *i64 292 var ci: i64 = 0 293 while ci < nk { 294 let le2: i64 = fx_line_end(bk, nk, ci) 295 if le2 > ci { 296 if fx_col(bk, ci, le2, FL_CK_CLASS, sp) == 1 { 297 if fx_slice_eqs(bk, sp[0], sp[1], cls) == 1 { 298 if fx_col_atoi(bk, ci, le2, FL_CK_MAND, sp) == 1 { 299 if fx_col(bk, ci, le2, 0, cs2) == 1 { 300 if fx_has_finding(bf, nf, deal, FL_CF_DEAL, FL_CF_COMP, bk, cs2[0], cs2[1], sp) == 0 { 301 n_missing = n_missing + 1 302 po = fx_bcat(pb, po, "UNINSPECTED " as *u8) 303 po = fx_bcatsl(pb, po, bk, cs2[0], cs2[1]) 304 po = fx_bcat(pb, po, " mandatory-system-no-finding\n" as *u8) 305 } 306 } 307 } 308 } 309 } 310 } 311 ci = le2 + 1 312 } 313 314 if n_seen == 0 { 315 fx_puts("FLIP-RED reason=no-findings-for-deal deal=" as *u8); fx_puts(deal); fx_puts("\n" as *u8) 316 fx_puts("verdict=REFUSED\n" as *u8) 317 fx_puts("rule=absent-inspection-is-not-a-clean-inspection\n" as *u8) 318 sys_exit(FX_EXIT_REFUSED) 319 return FX_EXIT_REFUSED 320 } 321 322 let holding: i64 = hold_day * days 323 let base: i64 = asking + recon + transport + fees + holding 324 let allin_typ: i64 = base + rep_t 325 // exposure is 0 whenever the wear pass is OFF, so this line is arithmetically identical to 326 // `base + rep_h` for every existing caller -- which is why the 23 gate assertions hold. 327 let allin_bad: i64 = base + rep_h + exposure 328 let rs_typ: i64 = resale_typ + lift_t 329 let rs_low: i64 = resale_low + lift_l 330 let sc_typ: i64 = rs_typ * sell_bps / FX_BPS 331 let sc_low: i64 = rs_low * sell_bps / FX_BPS 332 let profit_typ: i64 = rs_typ - sc_typ - allin_typ 333 let profit_p10: i64 = rs_low - sc_low - allin_bad 334 var roi_t: i64 = 0 335 if allin_typ > 0 { roi_t = profit_typ * FX_BPS / allin_typ } 336 var roi_p: i64 = 0 337 if allin_bad > 0 { roi_p = profit_p10 * FX_BPS / allin_bad } 338 339 var vcode: i64 = 0 340 if n_missing > 0 { vcode = 4 } else { if days > dts_cap { vcode = 2 } else { if profit_p10 >= gate_min { vcode = 1 } } } 341 342 fx_puts("FLIP-ASSESS\n" as *u8) 343 fx_puts("class=" as *u8); fx_puts(cls); fx_puts(" deal=" as *u8); fx_puts(deal); fx_puts("\n" as *u8) 344 fx_puts("verdict=" as *u8) 345 if vcode == 4 { fx_puts("REFUSED" as *u8) } 346 if vcode == 1 { fx_puts("BUY" as *u8) } 347 if vcode == 2 { fx_puts("PASS" as *u8) } 348 if vcode == 0 { fx_puts("PASS" as *u8) } 349 fx_puts(" uninspected_mandatory=" as *u8); fx_putn(n_missing) 350 fx_puts(" profit_p10=" as *u8); fx_putn(profit_p10) 351 fx_puts(" profit_typ=" as *u8); fx_putn(profit_typ) 352 fx_puts(" allin_bad=" as *u8); fx_putn(allin_bad) 353 fx_puts(" mand=" as *u8); fx_putn(n_mand) 354 fx_puts(" flip=" as *u8); fx_putn(n_flip) 355 fx_puts(" spec=" as *u8); fx_putn(n_spec) 356 fx_puts(" leave=" as *u8); fx_putn(n_leave) 357 fx_puts("\n" as *u8) 358 fx_puts("rule=" as *u8) 359 if vcode == 4 { fx_puts("mandatory-systems-never-inspected-absent-finding-is-not-a-clean-finding\n" as *u8) } 360 if vcode == 2 { fx_puts("hold-period-exceeds-class-days-to-sell-cap\n" as *u8) } 361 if vcode == 1 { fx_puts("P10>=gate_min downside-positive after mandatory repairs\n" as *u8) } 362 if vcode == 0 { fx_puts("P10<gate_min downside-negative walk-away\n" as *u8) } 363 364 fx_kv("parts_assessed" as *u8, n_seen) 365 fx_kv("parts_mandatory" as *u8, n_mand) 366 fx_kv("parts_flippoint" as *u8, n_flip) 367 fx_kv("parts_speculative" as *u8, n_spec) 368 fx_kv("parts_leave" as *u8, n_leave) 369 fx_kv("parts_uncatalogued" as *u8, n_unk) 370 fx_kv("uninspected_mandatory" as *u8, n_missing) 371 fx_kv("mandatory_cost_high" as *u8, mand_h) 372 fx_kv("flippoint_gain_p10" as *u8, flip_gain) 373 fx_kv("repair_taken_typ" as *u8, rep_t) 374 fx_kv("repair_taken_high" as *u8, rep_h) 375 fx_kv("lift_taken_typ" as *u8, lift_t) 376 fx_kv("lift_taken_low" as *u8, lift_l) 377 fx_kv("holding" as *u8, holding) 378 fx_kv("sell_cost_typ" as *u8, sc_typ) 379 fx_kv("sell_cost_low" as *u8, sc_low) 380 fx_kv("allin_typ" as *u8, allin_typ) 381 fx_kv("allin_bad" as *u8, allin_bad) 382 // Emitted ONLY when the wear pass ran, so an existing caller's output is byte-identical. 383 // wear_components_unpriced is the COVERAGE declaration: it says how much of the risk this 384 // number does NOT contain. A total without it would read as complete when it is not. 385 if n_obs_plane > 0 { 386 fx_kv("wear_usage_now" as *u8, usage_now) 387 fx_kv("wear_hold" as *u8, hz_hold) 388 fx_kv("wear_exposure_total" as *u8, exposure) 389 fx_kv("wear_components_priced" as *u8, n_priced) 390 fx_kv("wear_components_unpriced" as *u8, n_unpriced) 391 } 392 fx_kv("resale_eff_typ" as *u8, rs_typ) 393 fx_kv("resale_eff_low" as *u8, rs_low) 394 fx_kv("profit_typ" as *u8, profit_typ) 395 fx_kv("profit_p10" as *u8, profit_p10) 396 fx_kv("roi_typ_bps" as *u8, roi_t) 397 fx_kv("roi_p10_bps" as *u8, roi_p) 398 fx_kv("gate_min" as *u8, gate_min) 399 fx_kv("days" as *u8, days) 400 fx_kv("days_cap" as *u8, dts_cap) 401 402 sys_write(FX_OUT, pb, po) 403 sys_exit(0) 404 return 0 405}