code wiki / _hdl_build / nx_office_live_verify.nx
nx_office_live_verify.nx source
↩ module page · 229 lines · 13234 B
1// nx_office_live_verify.nx -- SOVEREIGN live verification of the PUBLIC Nishi Office app, every rung OURS on OUR
2// hardware: DNS -> TCP -> our TLS 1.3 handshake (X.509 vs the Mozilla trust store) -> HTTP GET/POST -> our HTTP
3// parser -> our dechunk -> byte assertions. REPLACES WebFetch / PowerShell Invoke-WebRequest (operator 2026-07-10:
4// "use the nishi apis"; [[feedback-verify-live-via-nishi-browser-not-webfetch]]). Clone of nx_survey_live_verify;
5// lv_fetch is byte-identical (the reusable bits-up request core), only the office assertions in main() differ.
6// GET /office -> 200 + Nishi Office home
7// POST /office/save (x2, A then B) -> 303 each (create/version the probe doc zz-live-verify)
8// GET /office/doc/zz-live-verify -> 200 + editable #ed + Restore column + diff link (>=2 versions)
9// GET /office/file/zz-live-verify/v1 -> 200 + "PK" (a real Word .docx, byte-produced by the gated organ)
10// GET /office/diff/zz-live-verify/1/2 -> 200 + "Changes: v1" + add/del rows (v1/v2 FROZEN forever -> stable)
11// POST /office/restore version=1 -> 303 (restore = additive new version; the never-lose exceed, live)
12// v1/v2 of zz-live-verify are frozen by the additive store, so these assertions are deterministic across runs even
13// as the probe doc accumulates versions. Exit 0 = the whole office served + interactive through our own browser
14// stack, no WebFetch. license_tier: ORIGINAL expect_exit: 0
15import "nx_syscalls.nx"
16import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
17import "nx_x509_trust_store.nx"
18import "nx_trust_store_load_from_certdata.nx"
19import "nx_tls13_client_validate_certificate.nx"
20import "nx_tls13_client_session_run.nx"
21import "nx_https_url_for_fetch.nx"
22import "nx_https_url_connect.nx"
23import "nx_https_get_complete.nx"
24import "nx_https_post_complete.nx"
25import "nx_http_response_parse.nx"
26const K_MAGIC_262144: i64 = 262144
27const K_MAGIC_16384: i64 = 16384
28const K_MAGIC_4194304: i64 = 4194304
29
30func lv_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
31// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
32// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
33// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
34// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
35func lv_n(v: i64) -> i64 { nxi_out(v); return 0 }
36func lv_check(pass: i64, label: *u8, fails: *i64) -> i64 {
37 lv_w(" " as *u8); lv_w(label); lv_w(": " as *u8)
38 if pass==1 { lv_w("PASS\n" as *u8) } else { lv_w("FAIL\n" as *u8); fails[0]=fails[0]+1 }
39 return 0
40}
41func lv_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
42func lv_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i]; i=i+1} return off+i }
43func lv_catb(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var i: i64=0; while i<n { dst[off+i]=src[i]; i=i+1 } return off+n }
44func lv_catnum(dst: *u8, off: i64, v: i64) -> i64 {
45 var o: i64=off
46 var m: i64=v
47 let t: *u8=sys_mmap(28)
48 var k: i64=0
49 if m<=0 { dst[o]=48 as u8; return o+1 }
50 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
51 var i: i64=0
52 while i<k { dst[o+i]=t[k-1-i]; i=i+1 }
53 return o+k
54}
55func lv_has(buf: *u8, n: i64, needle: *u8) -> i64 {
56 let m: i64 = lv_slen(needle)
57 if m==0 { return 0 }
58 var i: i64=0
59 while i+m<=n {
60 var k: i64=0
61 var hit: i64=1
62 while k<m { if buf[i+k]!=needle[k] { hit=0; k=m } else { k=k+1 } }
63 if hit==1 { return 1 }
64 i=i+1
65 }
66 return 0
67}
68
69// one bits-up request to nishifamily.com (byte-identical to nx_survey_live_verify's lv_fetch).
70func lv_fetch(store: *TrustStore, is_post: i64, path: *u8, plen: i64, body: *u8, blen: i64,
71 html: *u8, cap: i64, st: *i64) -> i64 {
72 let url: *u8 = "https://nishifamily.com/\x00"
73 let url_p: *NxUrl = nx_url_new()
74 let target_raw: *u8 = sys_mmap(32)
75 let target: *NxHttpsTarget = target_raw as *NxHttpsTarget
76 target.url = url_p
77 target.port = 0
78 if nx_https_url_for_fetch(url, target) != NX_HTTPS_URL_OK { return 0-41 }
79 let host: *u8 = url + target.url.host_off
80 let hlen: i64 = target.url.host_len
81 let fd_p: *i64 = sys_mmap(16) as *i64
82 if nx_https_url_connect(target, url, sys_now_realtime_sec(), fd_p) != NX_HTTPS_CONNECT_OK { return 0-42 }
83 let fd: i64 = *fd_p
84 sys_set_socket_timeout(fd, 12)
85 let vc_raw: *u8 = sys_mmap(64)
86 let vc: *TlsValidationContext = vc_raw as *TlsValidationContext
87 vc.store = store
88 vc.sni_host = host
89 vc.sni_host_len = hlen
90 vc.now_epoch = sys_now_realtime_sec()
91 let cr: *u8 = sys_mmap(32)
92 let priv: *u8 = sys_mmap(32)
93 var i: i64=0
94 while i<32 { cr[i]=(0xC0+i) as u8; priv[i]=(0xA0+i) as u8; i=i+1 }
95 let sr: i64 = nx_tls13_client_session_run(fd, host, hlen, cr, priv, vc)
96 if sr<=0 { sys_close(fd); return 0-(200+(0-sr)) }
97 let session: *Tls13ClientSession = sr as *Tls13ClientSession
98 let buf: *u8 = sys_mmap(K_MAGIC_262144)
99 var gc: i64 = 0
100 if is_post==1 {
101 let req: *u8 = sys_mmap(K_MAGIC_16384)
102 var ro: i64 = 0
103 ro = lv_cat(req, ro, "POST " as *u8)
104 ro = lv_catb(req, ro, path, plen)
105 ro = lv_cat(req, ro, " HTTP/1.1\r\nHost: " as *u8)
106 ro = lv_catb(req, ro, host, hlen)
107 ro = lv_cat(req, ro, "\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: " as *u8)
108 ro = lv_catnum(req, ro, blen)
109 ro = lv_cat(req, ro, "\r\nConnection: close\r\n\r\n" as *u8)
110 ro = lv_catb(req, ro, body, blen)
111 gc = nx_https_req_complete(session, fd, req, ro, buf, K_MAGIC_262144)
112 } else {
113 gc = nx_https_get_complete(session, fd, path, plen, host, hlen, buf, K_MAGIC_262144)
114 }
115 sys_close(fd)
116 if gc<0 { return 0-(100+(0-gc)) }
117 let resp: *i64 = sys_mmap(128) as *i64
118 if nx_http_response_parse(buf, gc, resp)!=0 { return 0-50 }
119 st[0] = resp[1]
120 let body_off: i64 = resp[6]
121 let body_kind: i64 = resp[8]
122 var hl: i64 = 0
123 if body_kind==2 { hl = nx_http_dechunk(buf+body_off, gc-body_off, html, cap) }
124 else { hl = gc-body_off; var ci: i64=0; while ci<hl { if ci<cap { html[ci]=buf[body_off+ci] } ci=ci+1 } }
125 return hl
126}
127
128func main() -> i64 {
129 let fails: *i64 = sys_mmap(16) as *i64
130 fails[0]=0
131 lv_w("=== nx_office_live_verify -- SOVEREIGN verify of nishifamily.com/office (our TLS, our parser, our hardware, no WebFetch) ===\n" as *u8)
132
133 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 300, K_MAGIC_4194304)
134 if r<=0 { lv_w("TRUST LOAD FAIL (run from nxc2)\n" as *u8); return 1 }
135 let store: *TrustStore = r as *TrustStore
136 lv_w(" CA roots=" as *u8); lv_n(trust_store_count(store)); lv_w("\n" as *u8)
137
138 let html: *u8 = sys_mmap(K_MAGIC_262144)
139 let st: *i64 = sys_mmap(16) as *i64
140
141 // ---- GET /office (home) ----
142 let g: i64 = lv_fetch(store, 0, "/office\x00" as *u8, 7, 0 as *u8, 0, html, K_MAGIC_262144, st)
143 if g<0 { lv_w(" GET /office stage FAIL code=" as *u8); lv_n(g); lv_w("\n" as *u8); fails[0]=fails[0]+1 }
144 else {
145 lv_w(" GET /office -> HTTP " as *u8); lv_n(st[0]); lv_w(" body=" as *u8); lv_n(g); lv_w(" bytes (over our TLS 1.3)\n" as *u8)
146 var t1: i64=0
147 if st[0]==200 { t1=1 }
148 lv_check(t1, "T1 GET /office returns 200 through the sovereign edge" as *u8, fails)
149 var t2: i64=1
150 if lv_has(html, g, "Nishi Office" as *u8)==0 { t2=0 }
151 lv_check(t2, "T2 home renders (Nishi Office app, not the fallback)" as *u8, fails)
152 }
153
154 // ---- POST two saves -> the probe doc zz-live-verify gets v(k), v(k+1) (v1/v2 FROZEN forever by the store) ----
155 let ba: *u8 = "name=zz-live-verify&kind=doc&spec=H+Live+Verify%0AP+Probe+A\x00" as *u8
156 let pa: i64 = lv_fetch(store, 1, "/office/save\x00" as *u8, 12, ba, lv_slen(ba), html, K_MAGIC_262144, st)
157 var t3a: i64=0
158 if pa>=0 { if st[0]==303 { t3a=1 } }
159 lv_check(t3a, "T3a POST /office/save (Probe A) -> 303 (create/version through our stack)" as *u8, fails)
160 let bb: *u8 = "name=zz-live-verify&kind=doc&spec=H+Live+Verify%0AP+Probe+B\x00" as *u8
161 let pb: i64 = lv_fetch(store, 1, "/office/save\x00" as *u8, 12, bb, lv_slen(bb), html, K_MAGIC_262144, st)
162 var t3b: i64=0
163 if pb>=0 { if st[0]==303 { t3b=1 } }
164 lv_check(t3b, "T3b POST /office/save (Probe B) -> 303 (second version, additive)" as *u8, fails)
165
166 // ---- GET the editor page: WYSIWYG surface + Restore column + diff link (>=2 versions guaranteed) ----
167 let d: i64 = lv_fetch(store, 0, "/office/doc/zz-live-verify\x00" as *u8, 26, 0 as *u8, 0, html, K_MAGIC_262144, st)
168 if d<0 { lv_w(" GET doc stage FAIL code=" as *u8); lv_n(d); lv_w("\n" as *u8); fails[0]=fails[0]+1 }
169 else {
170 var t4: i64=0
171 if st[0]==200 { if lv_has(html, d, "id='ed'" as *u8)==1 { if lv_has(html, d, "<th>Restore</th>" as *u8)==1 { if lv_has(html, d, "/office/diff/zz-live-verify/" as *u8)==1 { t4=1 } } } }
172 lv_check(t4, "T4 GET /office/doc = visual editor + Restore column + version diff link" as *u8, fails)
173 }
174
175 // ---- GET the real .docx of the FROZEN v1 (starts with the ZIP magic PK) ----
176 let fdoc: i64 = lv_fetch(store, 0, "/office/file/zz-live-verify/v1\x00" as *u8, 30, 0 as *u8, 0, html, K_MAGIC_262144, st)
177 if fdoc<0 { lv_w(" GET file stage FAIL code=" as *u8); lv_n(fdoc); lv_w("\n" as *u8); fails[0]=fails[0]+1 }
178 else {
179 var t5: i64=0
180 if st[0]==200 { if fdoc>=2 { if (html[0] as i64)==80 { if (html[1] as i64)==75 { t5=1 } } } }
181 lv_check(t5, "T5 GET /office/file/.../v1 = a real Word .docx (PK zip magic, organ-produced)" as *u8, fails)
182 }
183
184 // ---- GET the version diff v1 vs v2 (frozen -> deterministic: Probe A -> Probe B is 1 add + 1 del) ----
185 let dif: i64 = lv_fetch(store, 0, "/office/diff/zz-live-verify/1/2\x00" as *u8, 31, 0 as *u8, 0, html, K_MAGIC_262144, st)
186 if dif<0 { lv_w(" GET diff stage FAIL code=" as *u8); lv_n(dif); lv_w("\n" as *u8); fails[0]=fails[0]+1 }
187 else {
188 var t6: i64=0
189 if st[0]==200 { if lv_has(html, dif, "Changes: v1" as *u8)==1 { if lv_has(html, dif, "class='dadd'" as *u8)==1 { if lv_has(html, dif, "class='ddel'" as *u8)==1 { t6=1 } } } }
190 lv_check(t6, "T6 GET /office/diff/1/2 = line diff of two frozen versions (add + del rows)" as *u8, fails)
191 }
192
193 // ---- POST restore of v1 -> a NEW version (the additive never-lose exceed, live) ----
194 let br: *u8 = "name=zz-live-verify&version=1\x00" as *u8
195 let pr: i64 = lv_fetch(store, 1, "/office/restore\x00" as *u8, 15, br, lv_slen(br), html, K_MAGIC_262144, st)
196 var t7: i64=0
197 if pr>=0 { if st[0]==303 { t7=1 } }
198 lv_check(t7, "T7 POST /office/restore v1 -> 303 (restore = additive new version, never destroys)" as *u8, fails)
199
200 // ---- GET a nonexistent doc -> 404 that a STRICT client can FRAME (of_err now stamps Content-Length) ----
201 // Before the fix a CL-less error hung our own TLS reader; now it reads clean + carries the LOUD body.
202 let nf: i64 = lv_fetch(store, 0, "/office/doc/zznope-does-not-exist\x00" as *u8, 33, 0 as *u8, 0, html, K_MAGIC_262144, st)
203 var t8: i64=0
204 if nf>=0 { if st[0]==404 { if lv_has(html, nf, "LOUD:" as *u8)==1 { t8=1 } } }
205 lv_check(t8, "T8 GET nonexistent doc -> 404 framed (Content-Length) + LOUD body, no strict-client hang" as *u8, fails)
206
207 // ---- POST an AUTOSAVE (ajax=1) -> 200 OK v<N>, also framed (was CL-less) ----
208 let bx: *u8 = "name=zz-live-verify&kind=doc&spec=H+Live+Verify%0AP+Autosave&ajax=1\x00" as *u8
209 let px: i64 = lv_fetch(store, 1, "/office/save\x00" as *u8, 12, bx, lv_slen(bx), html, K_MAGIC_262144, st)
210 var t9: i64=0
211 if px>=0 { if st[0]==200 { if lv_has(html, px, "OK v" as *u8)==1 { t9=1 } } }
212 lv_check(t9, "T9 POST autosave (ajax=1) -> 200 'OK v<N>' framed (autosave path, sovereign-readable)" as *u8, fails)
213
214 // ---- POST /office/ai: IN-APP AI over OUR OWN sovereign 0.5B seat (:11434). 303 = the model drafted a NEW
215 // version (full sovereign convergence: our office off our own AI on our own hardware); 503 = the seat is down
216 // (still a correct framed office response). Either proves the endpoint + our stack. ~24 tok ~7s < edge timeout.
217 let ai: i64 = lv_fetch(store, 1, "/office/ai\x00" as *u8, 10, "name=zz-live-verify\x00" as *u8, 19, html, K_MAGIC_262144, st)
218 lv_w(" POST /office/ai -> HTTP " as *u8); lv_n(st[0]); lv_w("\n" as *u8)
219 var t10: i64=0
220 if ai>=0 { if st[0]==303 { t10=1 } }
221 if ai>=0 { if st[0]==503 { t10=1 } }
222 lv_check(t10, "T10 POST /office/ai -> 303 (sovereign 0.5B drafted a version) OR 503 (seat down), framed over our TLS" as *u8, fails)
223
224 lv_w(" fails=" as *u8); lv_n(fails[0]); lv_w("\n" as *u8)
225 if fails[0]==0 { lv_w("VERDICT: GREEN (nishifamily.com/office live + interactive -- create/version/edit/restore/diff/download + framed errors + in-app AI verified bits-up through OUR stack, no WebFetch)\n" as *u8); sys_exit(0) }
226 lv_w("VERDICT: RED\n" as *u8)
227 sys_exit(1)
228 return 1
229}