code wiki / _hdl_build / nx_sitegen_file_gate.nx
nx_sitegen_file_gate.nx source
↩ module page · 193 lines · 10515 B
1// nx_sitegen_file_gate.nx -- gate for the AUTONOMY CORE (nx_sitegen_parse). Proves a site can be authored as a
2// DATA FILE and built by an organ with no hardcoded blueprint and no AI: it reads web_assets/_sites/
3// andelinwest.site, parses it, builds it, and asserts the result is BYTE-IDENTICAL to a hardcoded-blueprint
4// build of the same content (so the file format faithfully drives the engine). Negative controls: an
5// in-memory blueprint with junk lines + an orphan item parses gracefully (skips them); an incomplete
6// blueprint (no search) is REFUSED through the same UX gate; parse+build is deterministic. Appends
7// "CMSGATE row=nx_sitegen_parse site-builder-file ... verdict=PASS" to knowledge/status/cms_gate.log ONLY on
8// all-pass. Exit 0 iff all pass. license_tier: ORIGINAL
9import "nx_sitegen_parse.nx"
10import "nx_sitegen.nx"
11import "nx_syscalls.nx"
12
13func fw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func fnum(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
15func fcat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o }
16func fcatnum(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{dst[o]=t[k-1-i];o=o+1;i=i+1} return o }
17
18func frow(id: i64, ok: i64, what: *u8) -> i64 {
19 fw("FILEROW " as *u8); fnum(id); fw(" " as *u8)
20 if ok==1 { fw("PASS " as *u8) } else { fw("FAIL " as *u8) }
21 fw(what); fw("\n" as *u8)
22 return ok
23}
24
25func flen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
26
27func fslurp(path: *u8, buf: *u8, cap: i64) -> i64 {
28 let fd: i64 = sys_openat_rd(path)
29 if fd < 0 { return 0 - 1 }
30 var total: i64 = 0
31 var go: i64 = 1
32 while go == 1 {
33 let r: i64 = sys_read(fd, buf + total, cap - total)
34 if r <= 0 { go = 0 }
35 if r > 0 { total = total + r }
36 if total >= cap { go = 0 }
37 }
38 sys_close(fd)
39 return total
40}
41
42func fcontains(hay: *u8, hn: i64, needle: *u8) -> i64 {
43 var nl: i64 = 0
44 while needle[nl] != (0 as u8) { nl = nl + 1 }
45 if nl == 0 { return 1 }
46 if nl > hn { return 0 }
47 let last: i64 = hn - nl
48 var i: i64 = 0
49 while i <= last {
50 var j: i64 = 0
51 var hit: i64 = 1
52 while j < nl {
53 if (hay[i+j] as i64) != (needle[j] as i64) { hit = 0; j = nl }
54 if hit == 1 { j = j + 1 }
55 }
56 if hit == 1 { return 1 }
57 i = i + 1
58 }
59 return 0
60}
61
62func fbytes_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
63 if an != bn { return 0 }
64 var i: i64 = 0
65 while i < an { if (a[i] as i64) != (b[i] as i64) { return 0 } i = i + 1 }
66 return 1
67}
68
69// parse a blueprint text + build it to path; returns output length (slurped into buf). title via title_out.
70func f_parse_build(text: *u8, tn: i64, path: *u8, buf: *u8, cap: i64, title_out: *u8, nblk_out: *i64) -> i64 {
71 let kind: *i64 = sys_mmap(8*32) as *i64
72 let a: *i64 = sys_mmap(8*32) as *i64
73 let b: *i64 = sys_mmap(8*32) as *i64
74 let c: *i64 = sys_mmap(8*32) as *i64
75 let d: *i64 = sys_mmap(8*32) as *i64
76 let list: *i64 = sys_mmap(8*32) as *i64
77 let lcount: *i64 = sys_mmap(8*32) as *i64
78 let arena: *u8 = sys_mmap(65536)
79 let nblk: i64 = sgp_parse(text, tn, kind, a, b, c, d, list, lcount, 32, arena, title_out, 511)
80 nblk_out[0] = nblk
81 let fd: i64 = sys_openat_wr(path, 0x1a4)
82 if fd >= 0 { sg_build_page(fd, title_out, kind, a, b, c, d, list, lcount, nblk); sys_close(fd) }
83 return fslurp(path, buf, cap)
84}
85
86func main() -> i64 {
87 // ---- FILE-DRIVEN build: read the .site data file, parse, build ----
88 let ftext: *u8 = sys_mmap(65536)
89 let ftn: i64 = fslurp("web_assets/_sites/andelinwest.site" as *u8, ftext, 65535)
90 let title1: *u8 = sys_mmap(512)
91 let nbo: *i64 = sys_mmap(16) as *i64
92 let buf1: *u8 = sys_mmap(262144)
93 let len1: i64 = f_parse_build(ftext, ftn, "/tmp/nx_site_fromfile.html" as *u8, buf1, 262143, title1, nbo)
94 let nblk1: i64 = nbo[0]
95
96 // ---- HARDCODED build of the SAME content (the ground truth to byte-match) ----
97 let kind: *i64 = sys_mmap(8*16) as *i64
98 let a: *i64 = sys_mmap(8*16) as *i64
99 let b: *i64 = sys_mmap(8*16) as *i64
100 let c: *i64 = sys_mmap(8*16) as *i64
101 let d: *i64 = sys_mmap(8*16) as *i64
102 let list: *i64 = sys_mmap(8*16) as *i64
103 let lcount: *i64 = sys_mmap(8*16) as *i64
104 kind[0]=SG_HEADER; a[0]="Andelin West" as *u8 as i64; b[0]="Free consultation" as *u8 as i64; c[0]="#consult" as *u8 as i64
105 kind[1]=SG_HERO
106 a[1]="Straightforward legal help for Utah families and businesses." as *u8 as i64
107 b[1]="Talk to a licensed Utah attorney. Most matters start with one short call." as *u8 as i64
108 c[1]="Start your free consultation" as *u8 as i64
109 d[1]="#consult" as *u8 as i64
110 kind[2]=SG_SEARCH; a[2]="/search" as *u8 as i64; b[2]="Search: divorce, will, injury, contract..." as *u8 as i64
111 kind[3]=SG_CARDS; a[3]="How we help" as *u8 as i64; lcount[3]=4
112 let cards: *i64 = sys_mmap(8*8) as *i64
113 cards[0]="Family Law" as *u8 as i64; cards[1]="Divorce, custody, and support, handled with care." as *u8 as i64
114 cards[2]="Estate Planning" as *u8 as i64; cards[3]="Wills, trusts, and probate done right the first time." as *u8 as i64
115 cards[4]="Business Law" as *u8 as i64; cards[5]="Formation, contracts, and disputes for Utah businesses." as *u8 as i64
116 cards[6]="Personal Injury" as *u8 as i64; cards[7]="Injured? We deal with the insurers so you can heal." as *u8 as i64
117 list[3]=cards as i64
118 kind[4]=SG_STEPS; a[4]="Resolve it in 3 steps" as *u8 as i64; lcount[4]=3
119 let steps: *i64 = sys_mmap(8*8) as *i64
120 steps[0]="Tell us what happened -- 2 minutes, no jargon." as *u8 as i64
121 steps[1]="We review and call you back, usually the same day." as *u8 as i64
122 steps[2]="We handle it -- clear updates at every stage." as *u8 as i64
123 list[4]=steps as i64
124 kind[5]=SG_TRUST; lcount[5]=3
125 let trust: *i64 = sys_mmap(8*8) as *i64
126 trust[0]="Licensed by the Utah State Bar" as *u8 as i64
127 trust[1]="Free initial consultation" as *u8 as i64
128 trust[2]="Serving Utah families since 2008" as *u8 as i64
129 list[5]=trust as i64
130 kind[6]=SG_FOOTER; a[6]="Andelin West, PLLC -- Utah -- (801) 555-0100" as *u8 as i64
131 let fd2: i64 = sys_openat_wr("/tmp/nx_site_hard.html" as *u8, 0x1a4)
132 var grade2: i64 = 0 - 1
133 if fd2 >= 0 { grade2 = sg_build_page(fd2, "Andelin West -- Utah Attorneys" as *u8, kind, a, b, c, d, list, lcount, 7); sys_close(fd2) }
134 let buf2: *u8 = sys_mmap(262144)
135 let len2: i64 = fslurp("/tmp/nx_site_hard.html" as *u8, buf2, 262143)
136
137 var pass: i64 = 0
138 var rows: i64 = 0
139 var ok: i64 = 0
140
141 // R0: the file built a non-empty page and parsed the expected 7 blocks
142 ok = 0; if len1 > 0 { if nblk1 == 7 { ok = 1 } }
143 rows=rows+1; pass=pass+frow(0, ok, "site built from .site DATA FILE; parsed 7 blocks" as *u8)
144
145 // R1: file-driven build is BYTE-IDENTICAL to the hardcoded build (the format faithfully drives the engine)
146 ok = fbytes_eq(buf1, len1, buf2, len2)
147 rows=rows+1; pass=pass+frow(1, ok, "file-driven build == hardcoded build, byte-for-byte (format is faithful)" as *u8)
148
149 // R2: the title parsed from the file is correct
150 ok = sgp_streq(title1, "Andelin West -- Utah Attorneys" as *u8)
151 rows=rows+1; pass=pass+frow(2, ok, "page title parsed correctly from the file" as *u8)
152
153 // R3 (NEG): junk lines + an orphan item parse gracefully (skipped); valid blocks still build
154 let junk: *u8 = "###\nweirdkind|foo|bar\ncard|Orphan|before any cards\nheader|B|CTA|#x\nhero|H|sub|CTA|#x\nsearch|/s|find\nsteps|S\nstep|one\ntrust|\nsig|s1\nfooter|F\n" as *u8
155 let tj: *u8 = sys_mmap(512)
156 let nbj: *i64 = sys_mmap(16) as *i64
157 let bufj: *u8 = sys_mmap(65536)
158 let lenj: i64 = f_parse_build(junk, flen(junk), "/tmp/nx_site_junk.html" as *u8, bufj, 65535, tj, nbj)
159 ok = 0; if nbj[0] == 6 { if lenj > 0 { if fcontains(bufj, lenj, "Orphan" as *u8) == 0 { ok = 1 } } }
160 rows=rows+1; pass=pass+frow(3, ok, "junk/unknown/orphan lines skipped; 6 valid blocks build; no orphan leak (neg)" as *u8)
161
162 // R4 (NEG): an incomplete blueprint (no search) is REFUSED through the same UX gate (0 bytes)
163 let incomplete: *u8 = "header|B|CTA|#x\nhero|H|sub|CTA|#x\nsteps|S\nstep|one\ntrust|\nsig|s\nfooter|F\n" as *u8
164 let ti: *u8 = sys_mmap(512)
165 let nbi: *i64 = sys_mmap(16) as *i64
166 let bufi: *u8 = sys_mmap(65536)
167 let leni: i64 = f_parse_build(incomplete, flen(incomplete), "/tmp/nx_site_incomplete.html" as *u8, bufi, 65535, ti, nbi)
168 ok = 0; if leni == 0 { ok = 1 }
169 rows=rows+1; pass=pass+frow(4, ok, "incomplete blueprint (no search) REFUSED through the UX gate, 0 bytes (neg)" as *u8)
170
171 // R5: parse+build is deterministic (same file -> byte-identical twice)
172 let title3: *u8 = sys_mmap(512)
173 let nb3: *i64 = sys_mmap(16) as *i64
174 let buf3: *u8 = sys_mmap(262144)
175 let len3: i64 = f_parse_build(ftext, ftn, "/tmp/nx_site_fromfile2.html" as *u8, buf3, 262143, title3, nb3)
176 ok = fbytes_eq(buf1, len1, buf3, len3)
177 rows=rows+1; pass=pass+frow(5, ok, "deterministic: same file -> byte-identical build twice" as *u8)
178
179 fw("NX-SITEGEN-FILE-GATE rows=" as *u8); fnum(rows); fw(" pass=" as *u8); fnum(pass); fw("\n" as *u8)
180 if pass == rows {
181 let line: *u8 = sys_mmap(256)
182 var off: i64 = fcat(line, 0, "CMSGATE row=nx_sitegen_parse site-builder-file rows=" as *u8)
183 off = fcatnum(line, off, rows)
184 off = fcat(line, off, " pass=" as *u8); off = fcatnum(line, off, pass)
185 off = fcat(line, off, " verdict=PASS\n" as *u8)
186 let gf: i64 = sys_openat_append("knowledge/status/cms_gate.log" as *u8, 0x1a4)
187 if gf >= 0 { sys_write(gf, line, off); sys_close(gf) }
188 fw("NX-SITEGEN-FILE-GATE verdict=PASS -- file-driven autonomous build recorded in cms_gate.log\n" as *u8)
189 sys_exit(0); return 0
190 }
191 fw("NX-SITEGEN-FILE-GATE verdict=FAIL -- NOT recorded (no fake-green)\n" as *u8)
192 sys_exit(1); return 1
193}