nx_formatlaw.nx source
↩ module page · 245 lines · 13616 B
1// nx_formatlaw.nx -- the SOURCE-LAW CLI over nx_formatlaw_lib: scan sources for durable legacy-format literals
2// (F1/F1J), third-party executable paths (F2) and a missing license_tier (F3); print every finding NAMED and
3// LOCATED; optionally file each one into the REVIEW QUEUE plane. Never blocks, never refuses a build.
4//
5// nx_formatlaw scan <file.nx>... findings per file + a FORMATLAW summary line per file, exit 0 clean / 1 flagged
6// nx_formatlaw tree <dir> [<dir>...] every .nx directly under each dir (the two source roots), same output
7// nx_formatlaw review <actor> <file.nx>... scan AND append one 7-col row per finding to knowledge/store/review-
8// (id=fl_<fnv>, dedupe by id so a re-run files nothing twice)
9// nx_formatlaw hook PreToolUse/Write pre-flight: reads the hook JSON on stdin, scans the
10// `content` about to be written when `file_path` ends .nx, prints the
11// findings as advisories, ALWAYS exits 0
12// license_tier: ORIGINAL No hw writes (Rule 26).
13import "nx_syscalls.nx"
14import "nx_formatlaw_lib.nx"
15import "nx_json_lib.nx"
16import "nx_store_seed_lib.nx"
17
18const FLC_OUT: i64 = 262144
19const FLC_IN: i64 = 4194304 // the hook payload (a whole source file inside JSON): sized from the largest emitter (~340 KB) x 12 headroom
20const FLC_PATH: i64 = 4096
21const FLC_LIT_SHOW: i64 = 120 // a literal is shown to this many bytes; the count is never trimmed
22const FLC_DENTS: i64 = 65536
23const FLC_ROW: i64 = 2048
24const FLC_CH_NL: i64 = 10
25const FLC_CH_TAB: i64 = 9
26const FLC_CH_DOT: i64 = 46
27const FLC_CH_SLASH: i64 = 47
28const FLC_REVIEW_STATUS: *u8 = "open"
29const FLC_REVIEW_SCOPE_PFX: *u8 = "formatlaw/"
30
31func flc_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
32func flc_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = o; while s[i] != (0 as u8) { d[p] = s[i]; p = p + 1; i = i + 1 } return p }
33func flc_catn(d: *u8, o: i64, s: *u8, a: i64, e: i64) -> i64 { var p: i64 = o; var i: i64 = a; while i < e { d[p] = s[i]; p = p + 1; i = i + 1 } return p }
34func flc_num(d: *u8, o: i64, v: i64) -> i64 {
35 var m: i64 = v
36 var p: i64 = o
37 if m < 0 { d[p] = 45 as u8; p = p + 1; m = 0 - m }
38 if m == 0 { d[p] = 48 as u8; return p + 1 }
39 let t: *u8 = sys_mmap(32)
40 var k: i64 = 0
41 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
42 var i: i64 = k - 1
43 while i >= 0 { d[p] = t[i]; p = p + 1; i = i - 1 }
44 sys_munmap(t, 32)
45 return p
46}
47func flc_hex(d: *u8, o: i64, v: i64) -> i64 {
48 var p: i64 = o
49 var k: i64 = 15
50 while k >= 0 { let nib: i64 = (v >> (k*4)) & 15; if nib < 10 { d[p] = (48 + nib) as u8 } else { d[p] = (87 + nib) as u8 } p = p + 1; k = k - 1 }
51 return p
52}
53func flc_ends(s: *u8, lit: *u8) -> i64 {
54 let n: i64 = flc_slen(s)
55 let l: i64 = flc_slen(lit)
56 if n < l { return 0 }
57 var i: i64 = 0
58 while i < l { if s[n - l + i] != lit[i] { return 0 } i = i + 1 }
59 return 1
60}
61
62// report the findings of one buffer; returns the finding total. When `plane` is non-null, files review rows.
63func flc_report(label: *u8, b: *u8, n: i64, out: *u8, hits: *i64, counts: *i64, plane: *u8, actor: *u8, filed: *i64) -> i64 {
64 let total: i64 = fl_scan(b, n, hits, counts)
65 let kept: i64 = fl_kept(total)
66 var o: i64 = 0
67 var h: i64 = 0
68 while h < kept {
69 let cls: i64 = hits[h*FL_HIT_WORDS]
70 let line: i64 = hits[h*FL_HIT_WORDS+1]
71 let ls: i64 = hits[h*FL_HIT_WORDS+2]
72 var le: i64 = hits[h*FL_HIT_WORDS+3]
73 o = flc_cat(out, o, "FORMATLAW class=" as *u8); o = flc_cat(out, o, fl_class_name(cls))
74 o = flc_cat(out, o, " file=" as *u8); o = flc_cat(out, o, label)
75 o = flc_cat(out, o, " line=" as *u8); o = flc_num(out, o, line)
76 o = flc_cat(out, o, " literal=" as *u8)
77 var show_e: i64 = le
78 if show_e - ls > FLC_LIT_SHOW { show_e = ls + FLC_LIT_SHOW }
79 if cls == FL_F3 { o = flc_cat(out, o, "(header)" as *u8) } else { out[o] = 34 as u8; o = o + 1; o = flc_catn(out, o, b, ls, show_e); if show_e < le { o = flc_cat(out, o, "..." as *u8) } out[o] = 34 as u8; o = o + 1 }
80 o = flc_cat(out, o, " remedy=" as *u8); o = flc_cat(out, o, fl_remedy(cls)); out[o] = FLC_CH_NL as u8; o = o + 1
81 if (plane as i64) != 0 { if cls != FL_F3 { // F3 is a census figure (thousands of files), not a per-file queue row
82 // one 7-col review row: id title sev status owner scope note (id = fl_<fnv(label + literal)>)
83 let row: *u8 = sys_mmap(FLC_ROW)
84 var idh: i64 = fl_fnv(label, 0, flc_slen(label), FL_FNV_OFFSET)
85 idh = fl_fnv(b, ls, le, idh)
86 idh = idh ^ (cls * FL_FNV_PRIME)
87 var r: i64 = flc_cat(row, 0, "fl_" as *u8); r = flc_hex(row, r, idh)
88 let idlen: i64 = r
89 r = flc_cat(row, r, "\t" as *u8)
90 r = flc_cat(row, r, fl_class_name(cls)); r = flc_cat(row, r, ": " as *u8)
91 if cls == FL_F3 { r = flc_cat(row, r, "no license_tier in " as *u8); r = flc_cat(row, r, label) } else { r = flc_catn(row, r, b, ls, show_e) }
92 r = flc_cat(row, r, "\t" as *u8); r = flc_num(row, r, fl_sev(cls))
93 r = flc_cat(row, r, "\t" as *u8); r = flc_cat(row, r, FLC_REVIEW_STATUS)
94 r = flc_cat(row, r, "\t" as *u8); r = flc_cat(row, r, actor)
95 r = flc_cat(row, r, "\t" as *u8); r = flc_cat(row, r, FLC_REVIEW_SCOPE_PFX); r = flc_cat(row, r, label); r = flc_cat(row, r, ":" as *u8); r = flc_num(row, r, line)
96 r = flc_cat(row, r, "\t" as *u8); r = flc_cat(row, r, fl_remedy(cls))
97 // dedupe: an id already in the plane is not filed again (idempotent re-scan)
98 var present: i64 = 0
99 let lp: *i64 = sys_mmap(16) as *i64
100 let pb: *u8 = sts_load_fit(plane, lp)
101 if (pb as i64) != 0 { if fl_contains(pb, 0, lp[0], row) == 0 { present = 0 } }
102 if (pb as i64) != 0 {
103 // match the id column only: a row starting with `fl_<hex>\t`
104 row[idlen] = 0 as u8
105 var i: i64 = 0
106 let pn: i64 = lp[0]
107 while i < pn {
108 var atstart: i64 = 0
109 if i == 0 { atstart = 1 } else { if pb[i-1] == (FLC_CH_NL as u8) { atstart = 1 } }
110 if atstart == 1 { if fl_starts(pb, i, pn, row) == 1 { if i + idlen < pn { if pb[i + idlen] == (FLC_CH_TAB as u8) { present = 1; i = pn } } } }
111 i = i + 1
112 }
113 row[idlen] = FLC_CH_TAB as u8
114 }
115 if present == 0 {
116 var rc: i64 = sts_append_fast_locked(plane, row, r)
117 if rc == (0 - 1) {
118 let seed: *u8 = sys_mmap(r + 2)
119 var k: i64 = 0
120 while k < r { seed[k] = row[k]; k = k + 1 }
121 seed[r] = FLC_CH_NL as u8
122 rc = sts_seed(plane, seed, r + 1)
123 }
124 if rc >= 0 { filed[0] = filed[0] + 1 }
125 o = flc_cat(out, o, " REVIEW-FILED id=fl_" as *u8); o = flc_hex(out, o, idh); o = flc_cat(out, o, " rc=" as *u8); o = flc_num(out, o, rc); out[o] = FLC_CH_NL as u8; o = o + 1
126 } else {
127 o = flc_cat(out, o, " REVIEW-PRESENT id=fl_" as *u8); o = flc_hex(out, o, idh); o = flc_cat(out, o, " (already queued)\n" as *u8)
128 }
129 sys_munmap(row, FLC_ROW)
130 } }
131 h = h + 1
132 }
133 o = flc_cat(out, o, "FORMATLAW file=" as *u8); o = flc_cat(out, o, label)
134 o = flc_cat(out, o, " f1=" as *u8); o = flc_num(out, o, counts[FL_F1])
135 o = flc_cat(out, o, " f1j=" as *u8); o = flc_num(out, o, counts[FL_F1J])
136 o = flc_cat(out, o, " f2=" as *u8); o = flc_num(out, o, counts[FL_F2])
137 o = flc_cat(out, o, " f3=" as *u8); o = flc_num(out, o, counts[FL_F3])
138 o = flc_cat(out, o, " total=" as *u8); o = flc_num(out, o, total)
139 o = flc_cat(out, o, " shown=" as *u8); o = flc_num(out, o, kept)
140 if kept < total { o = flc_cat(out, o, " (SHOWN IS A PREFIX OF THE COUNT)" as *u8) }
141 if total == 0 { o = flc_cat(out, o, " verdict=CLEAN\n" as *u8) } else { o = flc_cat(out, o, " verdict=FLAGGED (advisory: nothing is blocked; each finding names its remedy)\n" as *u8) }
142 sys_write(1, out, o)
143 return total
144}
145
146func flc_scan_path(path: *u8, out: *u8, hits: *i64, counts: *i64, plane: *u8, actor: *u8, filed: *i64) -> i64 {
147 let lp: *i64 = sys_mmap(16) as *i64
148 let b: *u8 = sys_read_file(path, lp)
149 if (b as i64) == 0 {
150 var o: i64 = flc_cat(out, 0, "FORMATLAW file=" as *u8); o = flc_cat(out, o, path); o = flc_cat(out, o, " verdict=UNREADABLE (not scanned; an unreadable source is not a clean one)\n" as *u8)
151 sys_write(1, out, o)
152 return 0 - 1
153 }
154 return flc_report(path, b, lp[0], out, hits, counts, plane, actor, filed)
155}
156
157func main(argc: i64, argv: *i64) -> i64 {
158 let out: *u8 = sys_mmap(FLC_OUT)
159 let hits: *i64 = sys_mmap(FL_MAX_HITS * FL_HIT_WORDS * 8) as *i64
160 let counts: *i64 = sys_mmap(FL_CLASSES * 8) as *i64
161 let filed: *i64 = sys_mmap(8) as *i64
162 if argc < 2 {
163 sys_write(1, "usage: nx_formatlaw scan <file.nx>... | tree <dir>... | review <actor> <file.nx>... | hook\n" as *u8, 88)
164 sys_exit(2); return 2
165 }
166 let verb: *u8 = argv[1] as *u8
167 var flagged: i64 = 0
168 var files: i64 = 0
169 if fl_starts(verb, 0, flc_slen(verb), "hook" as *u8) == 1 {
170 // PreToolUse/Write: {"tool_input":{"file_path":"...","content":"..."}} on stdin
171 let inb: *u8 = sys_mmap(FLC_IN)
172 var n: i64 = 0
173 var go: i64 = 1
174 while go == 1 { let r: i64 = sys_read(0, ((inb as i64) + n) as *u8, FLC_IN - n); if r <= 0 { go = 0 } else { n = n + r; if n >= FLC_IN { go = 0 } } }
175 let fp: *u8 = sys_mmap(FLC_PATH)
176 if jx_get_str(inb, n, 0, "file_path" as *u8, fp, FLC_PATH) < 0 { return 0 }
177 if flc_ends(fp, ".nx" as *u8) == 0 { return 0 }
178 let content: *u8 = sys_mmap(FLC_IN)
179 let ce: i64 = jx_get_str_raw(inb, n, 0, "content" as *u8, content, FLC_IN)
180 if ce < 0 { return 0 } // an Edit (old_string/new_string) carries no whole content: nothing to judge
181 let cn: i64 = flc_slen(content)
182 let t: i64 = flc_report(fp, content, cn, out, hits, counts, 0 as *u8, "hook" as *u8, filed)
183 if t > 0 { sys_write(1, " (pre-flight, advisory only: the write proceeds; file the finding in the review queue with nx_formatlaw review, or fix it now)\n" as *u8, 133) }
184 return 0
185 }
186 var plane: *u8 = 0 as *u8
187 var actor: *u8 = "nx_formatlaw" as *u8
188 var first: i64 = 2
189 if fl_starts(verb, 0, flc_slen(verb), "review" as *u8) == 1 {
190 if argc < 4 { sys_write(1, "usage: nx_formatlaw review <actor> <file.nx>...\n" as *u8, 48); sys_exit(2); return 2 }
191 plane = FL_REVIEW_PLANE
192 actor = argv[2] as *u8
193 first = 3
194 }
195 if fl_starts(verb, 0, flc_slen(verb), "tree" as *u8) == 1 {
196 let dents: *u8 = sys_mmap(FLC_DENTS)
197 let pathb: *u8 = sys_mmap(FLC_PATH)
198 var d: i64 = 2
199 while d < argc {
200 let dir: *u8 = argv[d] as *u8
201 let fd: i64 = sys_openat_rd(dir)
202 if fd >= 0 {
203 var go2: i64 = 1
204 while go2 == 1 {
205 let nr: i64 = sys_getdents64(fd, dents, FLC_DENTS)
206 if nr <= 0 { go2 = 0 } else {
207 var pos: i64 = 0
208 while pos < nr {
209 let rec: *u8 = ((dents as i64) + pos) as *u8
210 let reclen: i64 = dirent_reclen(rec)
211 let name: *u8 = dirent_name(rec)
212 if dirent_type(rec) == DT_REG { if flc_ends(name, ".nx" as *u8) == 1 {
213 var po: i64 = flc_cat(pathb, 0, dir)
214 if pathb[po - 1] != (FLC_CH_SLASH as u8) { pathb[po] = FLC_CH_SLASH as u8; po = po + 1 }
215 po = flc_cat(pathb, po, name); pathb[po] = 0 as u8
216 let t: i64 = flc_scan_path(pathb, out, hits, counts, plane, actor, filed)
217 files = files + 1
218 if t > 0 { flagged = flagged + 1 }
219 } }
220 if reclen <= 0 { pos = nr } else { pos = pos + reclen }
221 }
222 }
223 }
224 sys_close(fd)
225 }
226 d = d + 1
227 }
228 } else {
229 var a: i64 = first
230 while a < argc {
231 let t: i64 = flc_scan_path(argv[a] as *u8, out, hits, counts, plane, actor, filed)
232 files = files + 1
233 if t > 0 { flagged = flagged + 1 }
234 a = a + 1
235 }
236 }
237 var o: i64 = flc_cat(out, 0, "FORMATLAW-SUMMARY files=" as *u8); o = flc_num(out, o, files)
238 o = flc_cat(out, o, " flagged=" as *u8); o = flc_num(out, o, flagged)
239 o = flc_cat(out, o, " clean=" as *u8); o = flc_num(out, o, files - flagged)
240 if (plane as i64) != 0 { o = flc_cat(out, o, " review_rows_filed=" as *u8); o = flc_num(out, o, filed[0]); o = flc_cat(out, o, " plane=" as *u8); o = flc_cat(out, o, plane) }
241 o = flc_cat(out, o, " (advisory census: exit 1 means findings exist, never that a build was refused)\n" as *u8)
242 sys_write(1, out, o)
243 if flagged > 0 { sys_exit(1); return 1 }
244 return 0
245}