nx_afford_ruler.nx source
↩ module page · 240 lines · 10852 B
1// nx_afford_ruler.nx -- THE AFFORDABILITY RULER. Measures the ecosystem's affordability capability across a
2// 4-domain x 6-rung grid (Housing / Healthcare / Food-energy / Debt x R1 macro-anatomy / R2 ingest / R3 audit /
3// R4 reduce / R5 act / R6 measured) by reading knowledge/compare/affordability.matrix -- the same data SSOT that
4// nx_swcompare_evidence grounds. Every claimed cell is GROUNDED: the ruler opens the cited organ on disk and finds
5// the symbol; a claim whose symbol cannot be found is a LIE (mat=X) and is never counted. It emits the maturity
6// grid + the per-rung gap histogram = the BUILD ORDER, chosen by measurement, not taste. Self-gated with a liar-kill
7// (a real symbol grounds AND a bogus one does not, in the same organ) + non-vacuity (the strong domain outscores the
8// void domains) + completeness (24 cells). Reuses the proven ev_read/find idiom from nx_swcompare_evidence.
9// Build: _offc/nx_sov_build_run.elf nx_afford_ruler license_tier: ORIGINAL
10import "nx_syscalls.nx"
11
12func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
13func gn(v0: i64) -> i64 {
14 var v: i64 = v0; if v < 0 { sys_write(1, "-" as *u8, 1); v = 0 - v }
15 let b: *u8 = sys_mmap(24); var k: i64 = 0; if v == 0 { b[0] = 48 as u8; k = 1 }
16 while v > 0 { b[k] = (48 + (v % 10)) as u8; v = v / 10; k = k + 1 }
17 let o: *u8 = sys_mmap(24); var j: i64 = 0; while j < k { o[j] = b[k-1-j]; j = j + 1 } sys_write(1, o, k); return 0
18}
19func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
20
21// read a whole file into buf[0..cap); return bytes read, or -1 if it cannot be opened.
22func ev_read(path: *u8, buf: *u8, cap: i64) -> i64 {
23 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 }
24 var tot: i64 = 0
25 var go: i64 = 1
26 while go == 1 {
27 if tot >= cap { go = 0 } else {
28 let r: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot)
29 if r <= 0 { go = 0 } else { tot = tot + r }
30 }
31 }
32 sys_close(fd); return tot
33}
34
35// return 1 if needle occurs in buf[0..n), else 0.
36func ev_find(buf: *u8, n: i64, needle: *u8) -> i64 {
37 let nl: i64 = slen(needle)
38 var i: i64 = 0
39 var res: i64 = 0
40 while i + nl <= n {
41 var k: i64 = 0
42 var hit: i64 = 1
43 while k < nl { if buf[i + k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } }
44 if hit == 1 { res = 1; i = n + 1 } else { i = i + 1 }
45 }
46 return res
47}
48
49// ground a cell: read the organ, return 1 if the symbol is present in its source, else 0.
50func ev_ground(organ: *u8, symbol: *u8, src: *u8, cap: i64) -> i64 {
51 let sn: i64 = ev_read(organ, src, cap)
52 if sn <= 0 { return 0 }
53 return ev_find(src, sn, symbol)
54}
55
56// copy the kk-th (0-based) pipe-delimited field of line[0..ln) into dst; returns length.
57func field_k(line: *u8, ln: i64, kk: i64, dst: *u8, cap: i64) -> i64 {
58 var field: i64 = 0
59 var s: i64 = 0
60 var i: i64 = 0
61 var fstart: i64 = 0
62 var fend: i64 = 0
63 var got: i64 = 0
64 while i <= ln {
65 var is_end: i64 = 0
66 if i == ln { is_end = 1 } else { if line[i] == (124 as u8) { is_end = 1 } }
67 if is_end == 1 {
68 if field == kk { fstart = s; fend = i; got = 1 }
69 field = field + 1
70 s = i + 1
71 }
72 i = i + 1
73 }
74 var t: i64 = 0
75 if got == 1 {
76 var p: i64 = fstart
77 while p < fend { if t < cap - 1 { dst[t] = line[p]; t = t + 1 } p = p + 1 }
78 }
79 dst[t] = 0 as u8
80 return t
81}
82
83func dom_idx(ch: i64) -> i64 {
84 if ch == 72 { return 0 }
85 if ch == 77 { return 1 }
86 if ch == 70 { return 2 }
87 if ch == 68 { return 3 }
88 return 0 - 1
89}
90
91func main() -> i64 {
92 let CAP: i64 = 2097152
93 let src: *u8 = sys_mmap(CAP)
94 let mbuf: *u8 = sys_mmap(65536)
95 let line: *u8 = sys_mmap(2048)
96 let flabel: *u8 = sys_mmap(256)
97 let forgan: *u8 = sys_mmap(256)
98 let fsym: *u8 = sys_mmap(160)
99 let grid: *i64 = sys_mmap(8 * 24) as *i64
100 let dbuilt: *i64 = sys_mmap(8 * 4) as *i64
101 let rgap: *i64 = sys_mmap(8 * 6) as *i64
102
103 var gi: i64 = 0
104 while gi < 24 { grid[gi] = 0; gi = gi + 1 }
105 gi = 0
106 while gi < 4 { dbuilt[gi] = 0; gi = gi + 1 }
107 gi = 0
108 while gi < 6 { rgap[gi] = 0; gi = gi + 1 }
109
110 var rows: i64 = 0
111 var present: i64 = 0
112 var grounded: i64 = 0
113 var lie: i64 = 0
114 var have: i64 = 0
115 var part: i64 = 0
116 var absent: i64 = 0
117
118 let mn: i64 = ev_read("knowledge/compare/affordability.matrix" as *u8, mbuf, 65536)
119 if mn <= 0 { gp("AFFORD-RULER: knowledge/compare/affordability.matrix missing pass=0 fail=1 verdict=RED\n" as *u8); sys_exit(1); return 1 }
120
121 var i: i64 = 0
122 while i < mn {
123 var e: i64 = i
124 var go: i64 = 1
125 while go == 1 { if e >= mn { go = 0 } else { if mbuf[e] == (10 as u8) { go = 0 } else { e = e + 1 } } }
126 let ll: i64 = e - i
127 if ll > 0 {
128 let c0: i64 = mbuf[i] as i64
129 if c0 != 35 { if c0 != 64 {
130 var t: i64 = 0
131 while t < ll { if t < 2047 { line[t] = mbuf[i + t] } t = t + 1 }
132 line[ll] = 0 as u8
133 field_k(line, ll, 0, flabel, 256)
134 field_k(line, ll, 1, forgan, 256)
135 field_k(line, ll, 2, fsym, 160)
136 let dom: i64 = dom_idx(flabel[0] as i64)
137 let rung: i64 = (flabel[1] as i64) - 49
138 let kind: i64 = flabel[2] as i64
139 if dom >= 0 { if rung >= 0 { if rung <= 5 {
140 rows = rows + 1
141 var mat: i64 = 0
142 if kind == 71 { absent = absent + 1 }
143 if kind != 71 {
144 present = present + 1
145 let g: i64 = ev_ground(forgan, fsym, src, CAP)
146 if g == 1 { grounded = grounded + 1 }
147 if g == 1 { if kind == 72 { mat = 2; have = have + 1 } }
148 if g == 1 { if kind == 80 { mat = 1; part = part + 1 } }
149 if g == 0 { mat = 3; lie = lie + 1 }
150 }
151 grid[dom * 6 + rung] = mat
152 } } }
153 } }
154 }
155 i = e + 1
156 }
157
158 // aggregate: per-domain built (partial or have), per-rung gap (absent or lie)
159 var d: i64 = 0
160 while d < 4 {
161 var r: i64 = 0
162 while r < 6 {
163 let m: i64 = grid[d * 6 + r]
164 if m >= 1 { if m <= 2 { dbuilt[d] = dbuilt[d] + 1 } }
165 if m == 0 { rgap[r] = rgap[r] + 1 }
166 if m == 3 { rgap[r] = rgap[r] + 1 }
167 r = r + 1
168 }
169 d = d + 1
170 }
171
172 gp("=== nx_afford_ruler: the affordability capability grid -- top-down cost-anatomy meets bottom-up household reduction ===\n" as *u8)
173 gp(" grounded from knowledge/compare/affordability.matrix (each cell opens the cited organ + finds the symbol; a claim that cannot be found is a LIE, never counted)\n" as *u8)
174 gp(" cells: H=have P=partial .=absent X=claimed-but-ungrounded(lie)\n\n" as *u8)
175 gp(" DOMAIN R1anat R2ingst R3audit R4reduc R5act R6meas\n" as *u8)
176 d = 0
177 while d < 4 {
178 if d == 0 { gp(" Housing " as *u8) }
179 if d == 1 { gp(" Healthcare " as *u8) }
180 if d == 2 { gp(" Food+Energy " as *u8) }
181 if d == 3 { gp(" Debt+Credit " as *u8) }
182 var r: i64 = 0
183 while r < 6 {
184 let m: i64 = grid[d * 6 + r]
185 gp(" " as *u8)
186 if m == 0 { gp(". " as *u8) }
187 if m == 1 { gp("P " as *u8) }
188 if m == 2 { gp("H " as *u8) }
189 if m == 3 { gp("X " as *u8) }
190 r = r + 1
191 }
192 gp(" built=" as *u8); gn(dbuilt[d]); gp("/6\n" as *u8)
193 d = d + 1
194 }
195
196 gp("\n PER-RUNG GAP (how many of the 4 domains are ABSENT at each rung -- the systemic holes):\n" as *u8)
197 gp(" R1 macro-anatomy=" as *u8); gn(rgap[0]); gp("/4 R2 ingest=" as *u8); gn(rgap[1]); gp("/4 R3 audit=" as *u8); gn(rgap[2]); gp("/4\n" as *u8)
198 gp(" R4 reduce=" as *u8); gn(rgap[3]); gp("/4 R5 act=" as *u8); gn(rgap[4]); gp("/4 R6 measured=" as *u8); gn(rgap[5]); gp("/4\n" as *u8)
199
200 var best: i64 = 0
201 var bestv: i64 = rgap[0]
202 var rr: i64 = 1
203 while rr < 6 { if rgap[rr] > bestv { bestv = rgap[rr]; best = rr } rr = rr + 1 }
204 gp("\n ==> BUILD ORDER (chosen by measurement): rung R" as *u8); gn(best + 1)
205 if best == 0 { gp(" macro cost-anatomy" as *u8) }
206 if best == 1 { gp(" household ingest" as *u8) }
207 if best == 2 { gp(" audit" as *u8) }
208 if best == 3 { gp(" reduce" as *u8) }
209 if best == 4 { gp(" act" as *u8) }
210 if best == 5 { gp(" measured-vs-source" as *u8) }
211 gp(" is the most systemic gap (" as *u8); gn(bestv); gp("/4 domains absent) -- climb it first, across domains.\n" as *u8)
212
213 let total: i64 = 24
214 let permille: i64 = (have * 1000 + part * 500) / total
215 gp("\n COVERAGE: have=" as *u8); gn(have); gp(" partial=" as *u8); gn(part); gp(" absent=" as *u8); gn(absent)
216 gp(" lie=" as *u8); gn(lie); gp(" of " as *u8); gn(total); gp(" cells => " as *u8); gn(permille); gp(" permille (partial counts half; a LOW honest number is the roadmap, not a failure)\n" as *u8)
217
218 // ---- self-gate: completeness + liar-kill + non-vacuity + systemic-signal ----
219 var pass: i64 = 0
220 var fail: i64 = 0
221 if rows == 24 { pass = pass + 1 } else { fail = fail + 1; gp(" FAIL completeness rows=" as *u8); gn(rows); gp(" expected 24\n" as *u8) }
222
223 let realfound: i64 = ev_ground("runtime/nx_fin_audit.nx" as *u8, "aud_duplicates" as *u8, src, CAP)
224 let bogusfound: i64 = ev_ground("runtime/nx_fin_audit.nx" as *u8, "ZZ_NOT_A_REAL_SYMBOL_QQ" as *u8, src, CAP)
225 var a2: i64 = 0
226 if realfound == 1 { if bogusfound == 0 { a2 = 1 } }
227 if a2 == 1 { pass = pass + 1 } else { fail = fail + 1; gp(" FAIL grounding-discrimination (real symbol not found, or bogus symbol grounded)\n" as *u8) }
228
229 if lie == 0 { pass = pass + 1 } else { fail = fail + 1; gp(" FAIL lie>0 (a cited symbol did not ground -- fix the matrix citation)\n" as *u8) }
230
231 var a4: i64 = 0
232 if dbuilt[1] > dbuilt[0] { if dbuilt[1] > dbuilt[2] { a4 = 1 } }
233 if a4 == 1 { pass = pass + 1 } else { fail = fail + 1; gp(" FAIL non-vacuity (healthcare must outscore housing AND food+energy)\n" as *u8) }
234
235 if bestv >= 2 { pass = pass + 1 } else { fail = fail + 1; gp(" FAIL systemic-signal (no rung is a concentrated gap across domains)\n" as *u8) }
236
237 gp("\nAFFORD-RULER pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail)
238 if fail == 0 { gp(" verdict=GREEN (grid grounded, liar-killed, non-vacuous; the measured build order stands)\n" as *u8); sys_exit(0); return 0 }
239 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1
240}