nx_climbout_real_gate.nx source
↩ module page · 148 lines · 8794 B
1// nx_climbout_real_gate.nx -- R5b GATE: proves the climb-out plan is built from REAL engine outputs. Drives
2// nx_fin_audit (overcharges), nx_fin_finance (rate savings), nx_debt (avalanche savings), nx_bills (overdue)
3// on the operator's scenario, feeds the figures to cor_build -> co_plan, and asserts the plan reflects the
4// computed numbers + only includes applicable actions. Exits 0 iff ALL pass. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_money.nx"
7import "nx_fin_audit.nx"
8import "nx_fin_finance.nx"
9import "nx_debt_payoff.nx"
10import "nx_bills.nx"
11import "nx_bills_store.nx"
12import "nx_climbout.nx"
13import "nx_climbout_real.nx"
14
15func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
16func g_putn(v: i64) -> i64 {
17 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
18 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
19 let d: *u8 = sys_mmap(24); var k: i64 = 0
20 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
21 var j: i64 = k - 1
22 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
23 return 0
24}
25func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 {
26 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") }
27 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") }
28 return 0
29}
30func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 }
31
32func main() -> i64 {
33 let st: *i64 = sys_mmap(16) as *i64
34 st[0] = 0; st[1] = 0
35 let idx: *i64 = sys_mmap(8 * 16) as *i64
36 let amt: *i64 = sys_mmap(8 * 16) as *i64
37 let oc: *i64 = sys_mmap(16) as *i64
38
39 // ---- RECOVER: real medical-overbilling audit (the R5 cancer bill) ----
40 let codes: *i64 = sys_mmap(8 * 8) as *i64
41 let dates: *i64 = sys_mmap(8 * 8) as *i64
42 let units: *i64 = sys_mmap(8 * 8) as *i64
43 let billed: *i64 = sys_mmap(8 * 8) as *i64
44 seti(codes,0,99214); seti(dates,0,20250310); seti(units,0,1); seti(billed,0,40000)
45 seti(codes,1,96413); seti(dates,1,20250310); seti(units,1,1); seti(billed,1,200000)
46 seti(codes,2,96413); seti(dates,2,20250310); seti(units,2,1); seti(billed,2,200000)
47 seti(codes,3,36415); seti(dates,3,20250310); seti(units,3,1); seti(billed,3,15000)
48 seti(codes,4,85025); seti(dates,4,20250310); seti(units,4,8); seti(billed,4,80000)
49 let pairA: *i64 = sys_mmap(8 * 2) as *i64; let pairB: *i64 = sys_mmap(8 * 2) as *i64
50 seti(pairA,0,96413); seti(pairB,0,36415)
51 let mcode: *i64 = sys_mmap(8 * 2) as *i64; let mmax: *i64 = sys_mmap(8 * 2) as *i64
52 seti(mcode,0,85025); seti(mmax,0,3)
53 aud_duplicates(codes, dates, billed, 5, idx, amt, oc); let r1: i64 = oc[0]
54 aud_unbundle(codes, 5, pairA, pairB, 1, billed, idx, amt, oc); let r2: i64 = oc[0]
55 aud_mue(codes, units, billed, 5, mcode, mmax, 1, idx, amt, oc); let r3: i64 = oc[0]
56 let recover: i64 = mny_add(mny_add(r1, r2), r3)
57 chk("RECOVER from real audit = 265000 ($2,650)", recover, 265000, st)
58
59 // ---- CUT-COST: real financing optimizer ----
60 let aprs: *i64 = sys_mmap(8 * 8) as *i64; let fees: *i64 = sys_mmap(8 * 8) as *i64
61 let upf: *i64 = sys_mmap(8 * 8) as *i64; let lic: *i64 = sys_mmap(8 * 8) as *i64
62 seti(aprs,0,mny_rate_from_pct(24,1)); seti(fees,0,0); seti(upf,0,0); seti(lic,0,1)
63 seti(aprs,1,mny_rate_from_pct(11,1)); seti(fees,1,10000); seti(upf,1,0); seti(lic,1,1)
64 seti(aprs,2,mny_rate_from_pct(9,1)); seti(fees,2,5000); seti(upf,2,0); seti(lic,2,1)
65 seti(aprs,3,mny_rate_from_pct(4,1)); seti(fees,3,0); seti(upf,3,1); seti(lic,3,0)
66 let usury: i64 = mny_rate_from_pct(36,1)
67 let mc: *i64 = sys_mmap(16) as *i64
68 let bi: *i64 = sys_mmap(16) as *i64
69 let cur: i64 = fe_total_cost(2000000, 60000, aprs[0], fees[0], mc)
70 let best: i64 = fe_rank_best(2000000, 60000, aprs, fees, upf, lic, 4, usury, bi)
71 let cutcost: i64 = cur - best
72 chk("CUT-COST from real financing = 1013696 ($10,136.96)", cutcost, 1013696, st)
73
74 // ---- ATTACK: real debt avalanche savings ----
75 let bal: *i64 = sys_mmap(8 * 2) as *i64; let apr: *i64 = sys_mmap(8 * 2) as *i64; let minp: *i64 = sys_mmap(8 * 2) as *i64
76 seti(bal,0,1000000); seti(apr,0,mny_rate_from_pct(24,1)); seti(minp,0,20000)
77 seti(bal,1,200000); seti(apr,1,mny_rate_from_pct(6,1)); seti(minp,1,5000)
78 let avi: *i64 = sys_mmap(16) as *i64; let sni: *i64 = sys_mmap(16) as *i64
79 dbt_strategy(bal, apr, minp, 2, 50000, 0, avi)
80 dbt_strategy(bal, apr, minp, 2, 50000, 1, sni)
81 let attack: i64 = sni[0] - avi[0]
82 chk("ATTACK from real debt (avalanche saves) = 72725 ($727.25)", attack, 72725, st)
83
84 // ---- STABILIZE: real overdue bills ----
85 let bamt: *i64 = sys_mmap(8 * 8) as *i64; let bcad: *i64 = sys_mmap(8 * 8) as *i64
86 let bdue: *i64 = sys_mmap(8 * 8) as *i64; let bpaid: *i64 = sys_mmap(8 * 8) as *i64
87 seti(bamt,0,200000); seti(bcad,0,1); seti(bdue,0,1); seti(bpaid,0,1)
88 seti(bamt,1,180000); seti(bcad,1,6); seti(bdue,1,15); seti(bpaid,1,0)
89 seti(bamt,2,60000); seti(bcad,2,1); seti(bdue,2,1); seti(bpaid,2,1)
90 seti(bamt,3,30000); seti(bcad,3,3); seti(bdue,3,10); seti(bpaid,3,0)
91 seti(bamt,4,15000); seti(bcad,4,1); seti(bdue,4,20); seti(bpaid,4,0)
92 let oidx: *i64 = sys_mmap(8 * 8) as *i64
93 let overdue: i64 = bl_overdue(bdue, bpaid, 12, 5, oidx)
94 chk("STABILIZE: real overdue count = 1 (HOA)", overdue, 1, st)
95
96 // ---- BUILD THE REAL PLAN ----
97 let tiers: *i64 = sys_mmap(8 * 8) as *i64
98 let impacts: *i64 = sys_mmap(8 * 8) as *i64
99 let n: i64 = cor_build(overdue, 3500, recover, cutcost, attack, 0, tiers, impacts)
100 chk("plan has 5 real actions", n, 5, st)
101 let order: *i64 = sys_mmap(8 * 8) as *i64
102 co_plan(tiers, impacts, n, order)
103 chk("step 1 = stabilize (overdue)", tiers[order[0]], CO_STABILIZE, st)
104 chk("step 2 = recover (audit)", tiers[order[1]], CO_RECOVER, st)
105 chk("step 3 = cut-cost (refinance)", tiers[order[2]], CO_CUTCOST, st)
106 chk("step 4 = attack debt", tiers[order[3]], CO_ATTACKDEBT, st)
107 let total: i64 = co_total_impact(impacts, n)
108 g_puts(" [info] REAL total climb-out = "); g_putn(total); g_puts("c\n")
109 chk("REAL total climb-out = 1354921c ($13,549.21)", total, 1354921, st)
110
111 // ---- DYNAMIC: no overcharge -> the recover step is omitted ----
112 let n2: i64 = cor_build(overdue, 3500, 0, cutcost, attack, 0, tiers, impacts)
113 chk("no overcharge -> recover step dropped (4 actions)", n2, 4, st)
114
115 // ---- cor_dashboard convenience (what fw_render calls): person's debt + their REAL bills ----
116 // hermetic bill-store prefix "/tmp/corbills_<us>-"
117 let pfx: *u8 = sys_mmap(64)
118 var pp: i64 = 0
119 let pre: *u8 = "/tmp/corbills_\x00" as *u8
120 while pre[pp] != (0 as u8) { pfx[pp] = pre[pp]; pp = pp + 1 }
121 let us: i64 = sys_now_us()
122 let tbx: *u8 = sys_mmap(32); var mm: i64 = us; var kk: i64 = 0
123 if mm == 0 { tbx[0] = 0x30 as u8; kk = 1 }
124 while mm > 0 { tbx[kk] = (0x30 + (mm % 10)) as u8; mm = mm / 10; kk = kk + 1 }
125 var zz: i64 = kk - 1
126 while zz >= 0 { pfx[pp] = tbx[zz]; pp = pp + 1; zz = zz - 1 }
127 pfx[pp] = 0x2d as u8; pp = pp + 1; pfx[pp] = 0 as u8
128
129 let cd_t: *i64 = sys_mmap(8 * 8) as *i64; let cd_i: *i64 = sys_mmap(8 * 8) as *i64
130 let cd_o: *i64 = sys_mmap(8 * 8) as *i64; let cd_n: *i64 = sys_mmap(16) as *i64
131 // NO bills entered (respondent "nobody") -> representative fallback (overdue 1) -> the established $13,549.21
132 let cdtot: i64 = cor_dashboard(pfx, "nobody\x00" as *u8, 12, 2000000, mny_rate_from_pct(24, 1), 60000, cd_t, cd_i, cd_o, cd_n)
133 g_puts(" [info] cor_dashboard (no bills) total = "); g_putn(cdtot); g_puts("c, actions="); g_putn(cd_n[0]); g_puts("\n")
134 chk("cor_dashboard (no bills) = 1354921 ($13,549.21, representative stabilize)", cdtot, 1354921, st)
135 chk("cor_dashboard plan = 5 actions", cd_n[0], 5, st)
136
137 // person ENTERS 2 overdue bills (due day 5 + 10, both unpaid) -> today=15 -> stabilize uses REAL overdue=2
138 bs_add(pfx, "operator\x00" as *u8, 100000, 1, 5, BL_HOUSING, 0)
139 bs_add(pfx, "operator\x00" as *u8, 50000, 1, 10, BL_UTILITY, 0)
140 let cdtot2: i64 = cor_dashboard(pfx, "operator\x00" as *u8, 15, 2000000, mny_rate_from_pct(24, 1), 60000, cd_t, cd_i, cd_o, cd_n)
141 g_puts(" [info] cor_dashboard (2 REAL overdue) total = "); g_putn(cdtot2); g_puts("c\n")
142 chk("cor_dashboard runs on REAL bills: 2 overdue -> stabilize $70 -> total 1358421", cdtot2, 1358421, st)
143
144 g_puts("nx_climbout_real_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n")
145 if st[1] == 0 { g_puts("SITUATION R5b nx_climbout_real: GREEN\n"); return 0 }
146 g_puts("SITUATION R5b nx_climbout_real: RED\n")
147 return 1
148}