code wiki / (root) / nx_recon_gate.nx

nx_recon_gate.nx source

↩ module page · 136 lines · 5908 B

1// nx_recon_gate.nx -- F983 INDEPENDENT GATE for general three-way reconciliation. 2// Builds a real book on the ledger, then proves the reconciliation tells the TRUTH in every direction: 3// it balances when it should, it BREAKS when it should, it names WHICH leg broke, and above all it 4// refuses to round a one-cent variance away. 5// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 6 7import "nx_recon_lib.nx" 8 9func rg_puts(s: *u8) -> i64 { 10 var n: i64 = 0 11 while s[n] != (0 as u8) { n = n + 1 } 12 sys_write(1, s, n) 13 return 0 14} 15func rg_putn(v: i64) -> i64 { 16 let t: *u8 = sys_mmap(32) 17 var o: i64 = 0 18 var m: i64 = v 19 if m < 0 { t[o] = 45 as u8; o = o + 1; m = 0 - m } 20 let d: *u8 = sys_mmap(32) 21 var k: i64 = 0 22 if m == 0 { d[0] = 48 as u8; k = 1 } 23 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 24 var i: i64 = 0 25 while i < k { t[o] = d[k - 1 - i]; o = o + 1; i = i + 1 } 26 sys_write(1, t, o) 27 return 0 28} 29func rg_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 { 30 if got == want { 31 cnt[0] = cnt[0] + 1 32 rg_puts(" PASS " as *u8); rg_puts(name); rg_puts(" = " as *u8); rg_putn(got); rg_puts("\n" as *u8) 33 return 1 34 } 35 cnt[1] = cnt[1] + 1 36 rg_puts(" FAIL " as *u8); rg_puts(name); rg_puts(" got " as *u8); rg_putn(got) 37 rg_puts(" want " as *u8); rg_putn(want); rg_puts("\n" as *u8) 38 return 0 39} 40func rg_id(tag: *u8, nonce: i64, out: *u8) -> i64 { 41 var o: i64 = mt_catcopy(out, 0, tag) 42 o = mt_catn(out, o, nonce) 43 out[o] = 0 as u8 44 return o 45} 46 47func main(argc: i64, argv: *i64) -> i64 { 48 let pfx: *u8 = "knowledge/store/recongate-" as *u8 49 let nonce: i64 = sys_now_us() 50 let cnt: *i64 = sys_mmap(16) as *i64 51 cnt[0] = 0 52 cnt[1] = 0 53 54 // a book: two sub-accounts, one control account, funded from the bank 55 let s1: *u8 = sys_mmap(64) 56 let s2: *u8 = sys_mmap(64) 57 let ctl: *u8 = sys_mmap(64) 58 let bank: *u8 = sys_mmap(64) 59 rg_id("sub1_" as *u8, nonce, s1) 60 rg_id("sub2_" as *u8, nonce, s2) 61 rg_id("control_" as *u8, nonce, ctl) 62 rg_id("bank_" as *u8, nonce, bank) 63 64 let t1: *u8 = sys_mmap(64) 65 let t2: *u8 = sys_mmap(64) 66 let t3: *u8 = sys_mmap(64) 67 rg_id("r1_" as *u8, nonce, t1) 68 rg_id("r2_" as *u8, nonce, t2) 69 rg_id("r3_" as *u8, nonce, t3) 70 71 let accts: *i64 = sys_mmap(8 * 2) as *i64 72 accts[0] = s1 as i64 73 accts[1] = s2 as i64 74 let out: *i64 = sys_mmap(8 * 8) as *i64 75 76 rg_puts("NISHI-RECON-GATE (F983 general three-way reconciliation, exact cents, no tolerance band)\n" as *u8) 77 78 // fund sub-accounts $700.00 and $500.00; mirror the SAME total into the control account 79 led_xfer(pfx, t1, bank, s1, 70000, "posted" as *u8) 80 led_xfer(pfx, t2, bank, s2, 50000, "posted" as *u8) 81 led_xfer(pfx, t3, bank, ctl, 120000, "posted" as *u8) 82 83 // ---- R1: fully reconciled, all three legs $1200.00 ---- 84 rg_ck(cnt, "R1 verdict BALANCED (0)" as *u8, 85 rec_three_way(pfx, accts, 2, ctl, 120000, 0, 0, out), REC_BALANCED) 86 rg_ck(cnt, "R1a leg1 subsidiary total" as *u8, out[0], 120000) 87 rg_ck(cnt, "R1b leg2 control balance" as *u8, out[1], 120000) 88 rg_ck(cnt, "R1c leg3 adjusted bank" as *u8, out[2], 120000) 89 90 // ---- R2: THE ANTI-FUDGE TEST. One cent must NOT round away. ---- 91 rg_ck(cnt, "R2 ONE CENT variance is REPORTED not rounded" as *u8, 92 rec_three_way(pfx, accts, 2, ctl, 119999, 0, 0, out), REC_BOOK_VS_BANK) 93 rg_ck(cnt, "R2a the variance is exactly 1 cent" as *u8, out[4], 1) 94 rg_ck(cnt, "R2b rec_is_balanced REFUSES on one cent" as *u8, 95 rec_is_balanced(pfx, accts, 2, ctl, 119999, 0, 0), 0) 96 97 // ---- R3: timing differences RECONCILE a real bank difference (they do not hide it) ---- 98 // statement is $200.00 light because a deposit has not landed yet -> in-transit adjusts it back 99 rg_ck(cnt, "R3 deposit-in-transit reconciles the book" as *u8, 100 rec_three_way(pfx, accts, 2, ctl, 100000, 20000, 0, out), REC_BALANCED) 101 rg_ck(cnt, "R3a leg3 adjusted back to book" as *u8, out[2], 120000) 102 103 // statement is $300.00 heavy because a disbursement has not cleared -> outstanding subtracts it 104 rg_ck(cnt, "R4 outstanding disbursement reconciles the book" as *u8, 105 rec_three_way(pfx, accts, 2, ctl, 150000, 0, 30000, out), REC_BALANCED) 106 107 // ---- R5: a WRONG adjustment must NOT balance (adjustments are not a fudge lever) ---- 108 rg_ck(cnt, "R5 wrong in-transit does NOT balance" as *u8, 109 rec_three_way(pfx, accts, 2, ctl, 100000, 19999, 0, out), REC_BOOK_VS_BANK) 110 111 // ---- R6: subsidiary vs control break is detected and NAMED separately ---- 112 // drop one sub-account from the list -> subsidiary total no longer matches control 113 rg_ck(cnt, "R6 subsidiary-vs-control break DETECTED" as *u8, 114 rec_three_way(pfx, accts, 1, ctl, 120000, 0, 0, out), REC_SUB_VS_CTRL) 115 rg_ck(cnt, "R6a variance names the missing $500.00 sub-account" as *u8, out[3], 0 - 50000) 116 117 // ---- R7: BOTH legs broken reports BOTH, not just the first ---- 118 rg_ck(cnt, "R7 both legs broken reports BOTH (3)" as *u8, 119 rec_three_way(pfx, accts, 1, ctl, 119999, 0, 0, out), REC_BOTH) 120 121 // ---- R8: variance SIGN is directional, not absolute ---- 122 rg_ck(cnt, "R8 sign is directional: bank heavy -> negative" as *u8, 123 rec_three_way(pfx, accts, 2, ctl, 120001, 0, 0, out) - REC_BOOK_VS_BANK, 0) 124 rg_ck(cnt, "R8a variance is -1 (book below bank)" as *u8, out[4], 0 - 1) 125 126 rg_puts("nx_recon_gate: pass=" as *u8); rg_putn(cnt[0]) 127 rg_puts(" fail=" as *u8); rg_putn(cnt[1]); rg_puts("\n" as *u8) 128 if cnt[1] == 0 { 129 rg_puts("F983 nx_recon: VERDICT=GREEN (three legs, exact cents, one-cent variance never rounded away)\n" as *u8) 130 sys_exit(0) 131 return 0 132 } 133 rg_puts("F983 nx_recon: VERDICT=RED\n" as *u8) 134 sys_exit(1) 135 return 1 136}