nx_recon_gate.nx source
↩ module page · 143 lines · 6526 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 // FIXTURE MOVED OUT OF THE SWEPT STORE (2026-08-07). knowledge/store/ is walked every 600s by
49 // the nx_segguard beat; this gate's fixture is one of TEN measured as actually folded by it. A
50 // fold landing mid-run rewrites the manifest under the code being measured, so a RED could not
51 // be attributed. Proven on the sibling defect: the SAME code went RED on a knowledge/store
52 // fixture and GREEN 24/24 on a /tmp one -- the RED tracked the FIXTURE, not the code.
53 // Created at SETUP, not teardown: a teardown does not run when a run crashes.
54 sys_mkdir("/tmp/recongate\x00" as *u8, 0x1ed)
55 let pfx: *u8 = "/tmp/recongate/recongate-" as *u8
56 let nonce: i64 = sys_now_us()
57 let cnt: *i64 = sys_mmap(16) as *i64
58 cnt[0] = 0
59 cnt[1] = 0
60
61 // a book: two sub-accounts, one control account, funded from the bank
62 let s1: *u8 = sys_mmap(64)
63 let s2: *u8 = sys_mmap(64)
64 let ctl: *u8 = sys_mmap(64)
65 let bank: *u8 = sys_mmap(64)
66 rg_id("sub1_" as *u8, nonce, s1)
67 rg_id("sub2_" as *u8, nonce, s2)
68 rg_id("control_" as *u8, nonce, ctl)
69 rg_id("bank_" as *u8, nonce, bank)
70
71 let t1: *u8 = sys_mmap(64)
72 let t2: *u8 = sys_mmap(64)
73 let t3: *u8 = sys_mmap(64)
74 rg_id("r1_" as *u8, nonce, t1)
75 rg_id("r2_" as *u8, nonce, t2)
76 rg_id("r3_" as *u8, nonce, t3)
77
78 let accts: *i64 = sys_mmap(8 * 2) as *i64
79 accts[0] = s1 as i64
80 accts[1] = s2 as i64
81 let out: *i64 = sys_mmap(8 * 8) as *i64
82
83 rg_puts("NISHI-RECON-GATE (F983 general three-way reconciliation, exact cents, no tolerance band)\n" as *u8)
84
85 // fund sub-accounts $700.00 and $500.00; mirror the SAME total into the control account
86 led_xfer(pfx, t1, bank, s1, 70000, "posted" as *u8)
87 led_xfer(pfx, t2, bank, s2, 50000, "posted" as *u8)
88 led_xfer(pfx, t3, bank, ctl, 120000, "posted" as *u8)
89
90 // ---- R1: fully reconciled, all three legs $1200.00 ----
91 rg_ck(cnt, "R1 verdict BALANCED (0)" as *u8,
92 rec_three_way(pfx, accts, 2, ctl, 120000, 0, 0, out), REC_BALANCED)
93 rg_ck(cnt, "R1a leg1 subsidiary total" as *u8, out[0], 120000)
94 rg_ck(cnt, "R1b leg2 control balance" as *u8, out[1], 120000)
95 rg_ck(cnt, "R1c leg3 adjusted bank" as *u8, out[2], 120000)
96
97 // ---- R2: THE ANTI-FUDGE TEST. One cent must NOT round away. ----
98 rg_ck(cnt, "R2 ONE CENT variance is REPORTED not rounded" as *u8,
99 rec_three_way(pfx, accts, 2, ctl, 119999, 0, 0, out), REC_BOOK_VS_BANK)
100 rg_ck(cnt, "R2a the variance is exactly 1 cent" as *u8, out[4], 1)
101 rg_ck(cnt, "R2b rec_is_balanced REFUSES on one cent" as *u8,
102 rec_is_balanced(pfx, accts, 2, ctl, 119999, 0, 0), 0)
103
104 // ---- R3: timing differences RECONCILE a real bank difference (they do not hide it) ----
105 // statement is $200.00 light because a deposit has not landed yet -> in-transit adjusts it back
106 rg_ck(cnt, "R3 deposit-in-transit reconciles the book" as *u8,
107 rec_three_way(pfx, accts, 2, ctl, 100000, 20000, 0, out), REC_BALANCED)
108 rg_ck(cnt, "R3a leg3 adjusted back to book" as *u8, out[2], 120000)
109
110 // statement is $300.00 heavy because a disbursement has not cleared -> outstanding subtracts it
111 rg_ck(cnt, "R4 outstanding disbursement reconciles the book" as *u8,
112 rec_three_way(pfx, accts, 2, ctl, 150000, 0, 30000, out), REC_BALANCED)
113
114 // ---- R5: a WRONG adjustment must NOT balance (adjustments are not a fudge lever) ----
115 rg_ck(cnt, "R5 wrong in-transit does NOT balance" as *u8,
116 rec_three_way(pfx, accts, 2, ctl, 100000, 19999, 0, out), REC_BOOK_VS_BANK)
117
118 // ---- R6: subsidiary vs control break is detected and NAMED separately ----
119 // drop one sub-account from the list -> subsidiary total no longer matches control
120 rg_ck(cnt, "R6 subsidiary-vs-control break DETECTED" as *u8,
121 rec_three_way(pfx, accts, 1, ctl, 120000, 0, 0, out), REC_SUB_VS_CTRL)
122 rg_ck(cnt, "R6a variance names the missing $500.00 sub-account" as *u8, out[3], 0 - 50000)
123
124 // ---- R7: BOTH legs broken reports BOTH, not just the first ----
125 rg_ck(cnt, "R7 both legs broken reports BOTH (3)" as *u8,
126 rec_three_way(pfx, accts, 1, ctl, 119999, 0, 0, out), REC_BOTH)
127
128 // ---- R8: variance SIGN is directional, not absolute ----
129 rg_ck(cnt, "R8 sign is directional: bank heavy -> negative" as *u8,
130 rec_three_way(pfx, accts, 2, ctl, 120001, 0, 0, out) - REC_BOOK_VS_BANK, 0)
131 rg_ck(cnt, "R8a variance is -1 (book below bank)" as *u8, out[4], 0 - 1)
132
133 rg_puts("nx_recon_gate: pass=" as *u8); rg_putn(cnt[0])
134 rg_puts(" fail=" as *u8); rg_putn(cnt[1]); rg_puts("\n" as *u8)
135 if cnt[1] == 0 {
136 rg_puts("F983 nx_recon: VERDICT=GREEN (three legs, exact cents, one-cent variance never rounded away)\n" as *u8)
137 sys_exit(0)
138 return 0
139 }
140 rg_puts("F983 nx_recon: VERDICT=RED\n" as *u8)
141 sys_exit(1)
142 return 1
143}