nx_fin_spine_gate.nx source
↩ module page · 158 lines · 6345 B
1// nx_fin_spine_gate.nx -- THE FINANCE SPINE WITNESS. One organ that re-proves the load-bearing
2// property of every layer (ledger -> chart of accounts -> reconciliation -> statements) and
3// SELF-PUBLISHES its verdict to knowledge/status/fin_spine_gate.log so the sovereign maturity ruler
4// can LIVE-DERIVE the finance grade instead of anyone typing one.
5//
6// ★THE LOG IS TRUNCATED, NEVER APPENDED (sys_openat_wr = O_WRONLY_CREATE_TRUNC). If this were an
7// append-only log, an old VERDICT=GREEN would survive a later RED run and the ruler would read a
8// STALE PASS -- the exact liar the honest-maturity discipline exists to kill. The log always states
9// the CURRENT run and nothing else.
10//
11// This is an INDEPENDENT witness: it imports the libs and re-derives the properties itself rather
12// than trusting the per-organ gates. Two independent sources converging is what earns triangulation.
13// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
14
15import "nx_stmt_lib.nx"
16import "nx_recon_lib.nx"
17
18const FS_MODE: i64 = 420
19
20func fs_puts(s: *u8) -> i64 {
21 var n: i64 = 0
22 while s[n] != (0 as u8) { n = n + 1 }
23 sys_write(1, s, n)
24 return 0
25}
26func fs_cat(buf: *u8, off: i64, s: *u8) -> i64 {
27 var o: i64 = off
28 var j: i64 = 0
29 while s[j] != (0 as u8) { buf[o] = s[j]; o = o + 1; j = j + 1 }
30 return o
31}
32func fs_catn(buf: *u8, off: i64, v: i64) -> i64 {
33 var o: i64 = off
34 var m: i64 = v
35 if m < 0 { buf[o] = 45 as u8; o = o + 1; m = 0 - m }
36 let d: *u8 = sys_mmap(32)
37 var k: i64 = 0
38 if m == 0 { d[0] = 48 as u8; k = 1 }
39 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
40 var i: i64 = 0
41 while i < k { buf[o] = d[k - 1 - i]; o = o + 1; i = i + 1 }
42 return o
43}
44func fs_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 {
45 if got == want {
46 cnt[0] = cnt[0] + 1
47 fs_puts(" PASS " as *u8); fs_puts(name); fs_puts("\n" as *u8)
48 return 1
49 }
50 cnt[1] = cnt[1] + 1
51 fs_puts(" FAIL " as *u8); fs_puts(name); fs_puts("\n" as *u8)
52 return 0
53}
54func fs_id(tag: *u8, nonce: i64, out: *u8) -> i64 {
55 var o: i64 = mt_catcopy(out, 0, tag)
56 o = mt_catn(out, o, nonce)
57 out[o] = 0 as u8
58 return o
59}
60
61func main(argc: i64, argv: *i64) -> i64 {
62 let lp: *u8 = "knowledge/store/finspine-led-" as *u8
63 let cp: *u8 = "knowledge/store/finspine-coa-" as *u8
64 let nonce: i64 = sys_now_us()
65 let cnt: *i64 = sys_mmap(16) as *i64
66 cnt[0] = 0
67 cnt[1] = 0
68
69 fs_puts("NISHI-FIN-SPINE-GATE (independent witness across ledger/coa/recon/stmt)\n" as *u8)
70
71 let cash: *u8 = sys_mmap(64)
72 let cap2: *u8 = sys_mmap(64)
73 let ghost: *u8 = sys_mmap(64)
74 fs_id("cash_" as *u8, nonce, cash)
75 fs_id("capital_" as *u8, nonce, cap2)
76 fs_id("ghost_" as *u8, nonce, ghost)
77 let t1: *u8 = sys_mmap(64)
78 fs_id("fs1_" as *u8, nonce, t1)
79
80 coa_type_put(cp, "asset" as *u8, "debit" as *u8)
81 coa_type_put(cp, "equity" as *u8, "credit" as *u8)
82 coa_put(cp, cash, "asset" as *u8)
83 coa_put(cp, cap2, "equity" as *u8)
84 led_xfer(lp, t1, cash, cap2, 250000, "posted" as *u8)
85
86 // L1 LEDGER -- conservation cross-check (per-account sums total zero)
87 fs_ck(cnt, "L1 ledger conservation: per-account balances sum to zero" as *u8,
88 led_balance(lp, cash) + led_balance(lp, cap2), 0)
89 // L2 LEDGER -- a refused transfer leaves state untouched
90 let b0: i64 = led_balance(lp, cash)
91 led_xfer(lp, t1, cash, cash, 100, "posted" as *u8)
92 fs_ck(cnt, "L2 ledger self-transfer refused, state untouched" as *u8, led_balance(lp, cash), b0)
93
94 // C1 COA -- sign resolves per account kind
95 let one: *i64 = sys_mmap(16) as *i64
96 coa_natural(lp, cp, cash, one)
97 fs_ck(cnt, "C1 coa asset natural balance positive" as *u8, one[0], 250000)
98 // C2 COA -- fail-closed on an unregistered account
99 fs_ck(cnt, "C2 coa fail-closed on unregistered account" as *u8,
100 coa_natural(lp, cp, ghost, one), 0)
101
102 // R1 RECON -- a one-cent variance is never rounded away
103 let accts: *i64 = sys_mmap(8 * 1) as *i64
104 accts[0] = cash as i64
105 let ro: *i64 = sys_mmap(8 * 8) as *i64
106 rec_three_way(lp, accts, 1, cash, 0 - 250001, 0, 0, ro)
107 fs_ck(cnt, "R1 recon reports a one-cent variance (no tolerance band)" as *u8, ro[4], 1)
108
109 // S1 STMT -- the accounting equation is DERIVED and balances
110 let sa: *i64 = sys_mmap(8 * 2) as *i64
111 sa[0] = cash as i64
112 sa[1] = cap2 as i64
113 let bs: *i64 = sys_mmap(8 * 8) as *i64
114 fs_ck(cnt, "S1 stmt accounting equation derived and balanced" as *u8,
115 stmt_balance_sheet(lp, cp, sa, 2, bs), 1)
116 // S2 STMT -- fail-closed rather than silently omitting an unresolvable account
117 let sa2: *i64 = sys_mmap(8 * 3) as *i64
118 sa2[0] = cash as i64
119 sa2[1] = cap2 as i64
120 sa2[2] = ghost as i64
121 fs_ck(cnt, "S2 stmt refuses the statement rather than omit an account" as *u8,
122 stmt_balance_sheet(lp, cp, sa2, 3, bs), 0)
123
124 // ---- self-publish the verdict (TRUNCATING: the log states THIS run only) ----
125 let buf: *u8 = sys_mmap(4096)
126 var o: i64 = 0
127 o = fs_cat(buf, o, "NISHI-FIN-SPINE-GATE (ledger/coa/recon/stmt independent witness)\n" as *u8)
128 o = fs_cat(buf, o, "checks_pass=" as *u8)
129 o = fs_catn(buf, o, cnt[0])
130 o = fs_cat(buf, o, " checks_fail=" as *u8)
131 o = fs_catn(buf, o, cnt[1])
132 o = fs_cat(buf, o, "\nscope=F981 ledger + F982 coa + F983 recon + F985 stmt\n" as *u8)
133 if cnt[1] == 0 { o = fs_cat(buf, o, "VERDICT=GREEN\n" as *u8) }
134 if cnt[1] != 0 { o = fs_cat(buf, o, "VERDICT=RED\n" as *u8) }
135 let lg: i64 = sys_openat_wr("knowledge/status/fin_spine_gate.log" as *u8, FS_MODE)
136 if lg >= 0 {
137 sys_write(lg, buf, o)
138 sys_close(lg)
139 fs_puts(" published knowledge/status/fin_spine_gate.log\n" as *u8)
140 }
141 if lg < 0 { fs_puts(" WARN could not publish the evidence log\n" as *u8) }
142
143 fs_puts("nx_fin_spine_gate: pass=" as *u8)
144 let pb: *u8 = sys_mmap(64)
145 var po: i64 = fs_catn(pb, 0, cnt[0])
146 po = fs_cat(pb, po, " fail=" as *u8)
147 po = fs_catn(pb, po, cnt[1])
148 pb[po] = 10 as u8
149 sys_write(1, pb, po + 1)
150 if cnt[1] == 0 {
151 fs_puts("FIN-SPINE: VERDICT=GREEN\n" as *u8)
152 sys_exit(0)
153 return 0
154 }
155 fs_puts("FIN-SPINE: VERDICT=RED\n" as *u8)
156 sys_exit(1)
157 return 1
158}