nx_ingest_audit.nx source
↩ module page · 224 lines · 10333 B
1// nx_ingest_audit.nx -- THE MECHANIZED INGEST AUDIT (ladder + facts, one call per artifact).
2//
3// nx_ingest_audit <file>
4//
5// Detects the artifact's LANE from its bytes, runs the matching fact probe IN-PROCESS (the
6// existing gated libs -- zero new parsers), then prints the lane's LADDER rows from
7// knowledge/ladder_grades.conf (grades are DATA with evidence pointers, rule 11) and names every
8// non-CLAIMED rung as the lane's open work. ★A CAPABILITY REPORT THAT ONLY NAMES WHAT IT LOOKED
9// FOR CANNOT TELL YOU WHAT YOU HAVE (banked law): an unknown artifact says so explicitly, and a
10// lane MISSING from the conf is a LOUD RED (fail-closed), never an empty success.
11// Composes: nx_mediafacts_lib (8/8) - nx_modelfacts_lib (9/9) - nx_varfacts_lib (6/6).
12// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
13import "nx_syscalls.nx"
14import "nx_mediafacts_lib.nx"
15import "nx_modelfacts_lib.nx"
16import "nx_varfacts_lib.nx"
17import "nx_kkfacts_lib.nx" // koikatsu cards ARE pngs -- the card check must run BEFORE media
18
19const IA_WINDOW: i64 = 4194304
20const IA_GRADES: *u8 = "knowledge/ladder_grades.conf"
21
22func ia_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23func ia_w(s: *u8) -> i64 { sys_write(1, s, ia_slen(s)); return 0 }
24func ia_e(s: *u8) -> i64 { sys_write(2, s, ia_slen(s)); return 0 }
25func ia_num(v: i64) -> i64 {
26 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
27 var m: i64 = v
28 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
29 let t: *u8 = sys_mmap(32)
30 var k: i64 = 0
31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
32 let o: *u8 = sys_mmap(32)
33 var i: i64 = 0
34 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
35 sys_write(1, o, k)
36 return 0
37}
38
39// window read + drain (true size) + tail capture for zip-family lanes.
40// buf gets the HEAD window; tailbuf gets the LAST min(size, IA_WINDOW) bytes when the file is
41// bigger than the window (zip central directories live at the end).
42func ia_read(path: *u8, buf: *u8, tailbuf: *u8, sizebox: *i64, tailn: *i64) -> i64 {
43 let fd: i64 = sys_openat_rd(path)
44 if fd < 0 { return 0 - 1 }
45 let size: i64 = sys_lseek(fd, 0, 2)
46 if size < 0 { sys_close(fd); return 0 - 1 }
47 sizebox[0] = size
48 sys_lseek(fd, 0, 0)
49 var want: i64 = size
50 if want > IA_WINDOW { want = IA_WINDOW }
51 var got: i64 = 0
52 var stop: i64 = 0
53 while stop == 0 {
54 if got >= want { stop = 1 } else {
55 let k: i64 = sys_read(fd, ((buf as i64) + got) as *u8, want - got)
56 if k <= 0 { stop = 1 } else { got = got + k }
57 }
58 }
59 // tail: same window from the end (equal to head when the file fits entirely)
60 var tw: i64 = size
61 if tw > IA_WINDOW { tw = IA_WINDOW }
62 sys_lseek(fd, size - tw, 0)
63 var tg: i64 = 0
64 stop = 0
65 while stop == 0 {
66 if tg >= tw { stop = 1 } else {
67 let k2: i64 = sys_read(fd, ((tailbuf as i64) + tg) as *u8, tw - tg)
68 if k2 <= 0 { stop = 1 } else { tg = tg + k2 }
69 }
70 }
71 sys_close(fd)
72 tailn[0] = tg
73 return got
74}
75
76// print the ladder rows for a lane from a grades conf at `path`. Returns rows printed, 0 = lane
77// absent, -1 = conf unreadable. Parameterized so the gate drives it with a fixture conf.
78func ia_ladder_from(path: *u8, lane: *u8) -> i64 {
79 let lp: *i64 = sys_mmap(16) as *i64
80 let cf: *u8 = sys_read_file(path, lp)
81 if (cf as i64) == 0 { return 0 - 1 }
82 let n: i64 = lp[0]
83 let ll: i64 = ia_slen(lane)
84 var rows: i64 = 0
85 var gaps: i64 = 0
86 var pos: i64 = 0
87 while pos < n {
88 let ls: i64 = pos
89 var le: i64 = pos
90 var sc: i64 = 0
91 while sc == 0 {
92 if le >= n { sc = 1 } else { if (cf[le] as i64) == 10 { sc = 1 } else { le = le + 1 } }
93 }
94 pos = le + 1
95 if le > ls { if (cf[ls] as i64) != 35 {
96 var mm: i64 = 1
97 var c: i64 = 0
98 while c < ll { if ls + c >= le { mm = 0; c = ll } else { if (cf[ls+c] as i64) != (lane[c] as i64) { mm = 0; c = ll } else { c = c + 1 } } }
99 if mm == 1 { if ls + ll < le { if (cf[ls+ll] as i64) == 9 {
100 ia_w("AUDIT-LADDER " as *u8)
101 sys_write(1, ((cf as i64) + ls) as *u8, le - ls)
102 ia_w("\n" as *u8)
103 rows = rows + 1
104 // a non-CLAIMED state is open work: the state field is column 3
105 var tabs: i64 = 0
106 var sp: i64 = ls
107 var stc: i64 = 0 - 1
108 while sp < le { if (cf[sp] as i64) == 9 { tabs = tabs + 1; if tabs == 2 { stc = sp + 1; sp = le } } sp = sp + 1 }
109 if stc > 0 { if (cf[stc] as i64) != 67 { gaps = gaps + 1 } } // not 'C'LAIMED
110 } } }
111 } }
112 }
113 if rows > 0 { ia_w("AUDIT-OPEN-RUNGS " as *u8); ia_num(gaps); ia_w(" of " as *u8); ia_num(rows); ia_w(" (non-CLAIMED states above = the lane's build order)\n" as *u8) }
114 return rows
115}
116func ia_ladder(lane: *u8) -> i64 { return ia_ladder_from(IA_GRADES, lane) }
117
118func ia_ends(name: *u8, suf: *u8) -> i64 {
119 let nl: i64 = ia_slen(name)
120 let m: i64 = ia_slen(suf)
121 if nl < m { return 0 }
122 var i: i64 = 0
123 while i < m { if (name[nl-m+i] as i64) != (suf[i] as i64) { return 0 } i = i + 1 }
124 return 1
125}
126
127func main(argc: i64, argv: *i64) -> i64 {
128 if argc < 2 {
129 ia_e("usage: nx_ingest_audit <file> -- facts + ladder grade + open rungs, one call\n" as *u8)
130 sys_exit(2)
131 return 2
132 }
133 let path: *u8 = argv[1] as *u8
134 let buf: *u8 = sys_mmap(IA_WINDOW)
135 let tailb: *u8 = sys_mmap(IA_WINDOW)
136 let sizebox: *i64 = sys_mmap(16) as *i64
137 let tailn: *i64 = sys_mmap(16) as *i64
138 let n: i64 = ia_read(path, buf, tailb, sizebox, tailn)
139 if n < 0 { ia_e("ERROR: cannot open file\n" as *u8); sys_exit(1); return 1 }
140
141 var lane: *u8 = "unknown" as *u8
142 // koikatsu card? (a card IS a png -- the more specific lane wins the dispatch)
143 let kkf: *i64 = sys_mmap(KK_N_SLOTS * 8) as *i64
144 kk_probe(buf, n, sizebox[0], kkf)
145 if kkf[0] == 1 {
146 lane = "koikatsu" as *u8
147 ia_w("AUDIT-LANE koikatsu file_bytes=" as *u8); ia_num(sizebox[0]); ia_w("\n" as *u8)
148 ia_w("AUDIT-FACT portrait_px=" as *u8); ia_num(kkf[2]); ia_w("x" as *u8); ia_num(kkf[3])
149 ia_w(" payload_bytes=" as *u8); ia_num(kkf[4])
150 if kkf[6] >= 0 { ia_w(" coordinate=" as *u8); ia_num(kkf[6]) }
151 ia_w("\n" as *u8)
152 let rows0: i64 = ia_ladder(lane)
153 if rows0 <= 0 { ia_e("AUDIT-RED lane 'koikatsu' missing from knowledge/ladder_grades.conf\n" as *u8); sys_exit(4); return 4 }
154 sys_exit(0)
155 return 0
156 }
157 // media?
158 let mfacts: *i64 = sys_mmap(MF_N_SLOTS * 8) as *i64
159 mf_probe(buf, n, mfacts)
160 if mfacts[0] != MF_FMT_UNKNOWN {
161 lane = "media" as *u8
162 ia_w("AUDIT-LANE media file_bytes=" as *u8); ia_num(sizebox[0]); ia_w("\n" as *u8)
163 ia_w("AUDIT-FACT format_id=" as *u8); ia_num(mfacts[0])
164 if mfacts[1] >= 0 { ia_w(" width_px=" as *u8); ia_num(mfacts[1]) }
165 if mfacts[2] >= 0 { ia_w(" height_px=" as *u8); ia_num(mfacts[2]) }
166 if mfacts[5] >= 0 { ia_w(" sample_rate_hz=" as *u8); ia_num(mfacts[5]) }
167 ia_w("\n" as *u8)
168 } else {
169 // model?
170 let dfacts: *i64 = sys_mmap(MDL_N_SLOTS * 8) as *i64
171 mdl_probe(buf, n, sizebox[0], dfacts)
172 if dfacts[0] != MDL_FMT_UNKNOWN {
173 lane = "model" as *u8
174 ia_w("AUDIT-LANE model file_bytes=" as *u8); ia_num(sizebox[0]); ia_w("\n" as *u8)
175 ia_w("AUDIT-FACT format_id=" as *u8); ia_num(dfacts[0])
176 if dfacts[1] >= 0 { ia_w(" tris=" as *u8); ia_num(dfacts[1]) }
177 if dfacts[2] >= 0 { ia_w(" verts=" as *u8); ia_num(dfacts[2]) }
178 if dfacts[7] >= 0 { ia_w(" glb_accessors=" as *u8); ia_num(dfacts[7]) }
179 if dfacts[4] >= 0 { ia_w(" uv_present=" as *u8); ia_num(dfacts[4]) }
180 if dfacts[9] >= 0 { ia_w(" vrm_present=" as *u8); ia_num(dfacts[9]) }
181 ia_w("\n" as *u8)
182 } else {
183 // var package? (zip tail + .var suffix, or any zip whose CD parses with meta.json)
184 let vfacts: *i64 = sys_mmap(VF_N_SLOTS * 8) as *i64
185 vf_probe(tailb, tailn[0], sizebox[0], vfacts)
186 var is_var: i64 = 0
187 if vfacts[0] == 1 { if ia_ends(path, ".var" as *u8) == 1 { is_var = 1 } else { if vfacts[9] == 1 { is_var = 1 } } }
188 if is_var == 1 {
189 lane = "var" as *u8
190 ia_w("AUDIT-LANE var file_bytes=" as *u8); ia_num(sizebox[0]); ia_w("\n" as *u8)
191 ia_w("AUDIT-FACT entries=" as *u8); ia_num(vfacts[1])
192 ia_w(" morphs=" as *u8); ia_num(vfacts[2])
193 ia_w(" textures=" as *u8); ia_num(vfacts[4])
194 ia_w(" scenes=" as *u8); ia_num(vfacts[3]); ia_w("\n" as *u8)
195 } else {
196 // doc? (%PDF magic or epub/zip suffix)
197 var is_doc: i64 = 0
198 if n >= 4 { if (buf[0] as i64) == 37 { if (buf[1] as i64) == 80 { if (buf[2] as i64) == 68 { if (buf[3] as i64) == 70 { is_doc = 1 } } } } }
199 if ia_ends(path, ".epub" as *u8) == 1 { if vfacts[0] == 1 { is_doc = 1 } }
200 if is_doc == 1 {
201 lane = "doc" as *u8
202 ia_w("AUDIT-LANE doc file_bytes=" as *u8); ia_num(sizebox[0])
203 ia_w(" (facts rung D1 open -- the grade rows below say exactly that)\n" as *u8)
204 }
205 }
206 }
207 }
208
209 if ia_slen(lane) == 7 {
210 ia_w("AUDIT-LANE unknown file_bytes=" as *u8); ia_num(sizebox[0]); ia_w("\n" as *u8)
211 ia_w("AUDIT-HONEST this auditor cannot tell you what this artifact is -- it checked media (png/gif/jpeg/webp/wav), model (glb/stl/obj/ply), var (zip manifest), doc (pdf/epub); anything else is beyond its current probes BY CONSTRUCTION, not absent from the world\n" as *u8)
212 sys_exit(0)
213 return 0
214 }
215
216 let rows: i64 = ia_ladder(lane)
217 if rows <= 0 {
218 ia_e("AUDIT-RED lane '" as *u8); ia_e(lane); ia_e("' has NO rows in knowledge/ladder_grades.conf -- a missing grade table is a loud failure, never an empty success\n" as *u8)
219 sys_exit(4)
220 return 4
221 }
222 sys_exit(0)
223 return 0
224}