code wiki / _hdl_build / nx_lastmile_ratchet.nx
nx_lastmile_ratchet.nx source
↩ module page · 103 lines · 5308 B
1// nx_lastmile_ratchet.nx -- LAST-MILE THINNESS RATCHET (operator 2026-07-02: "make sure we arent
2// building into third party systems like js ... last mile emitting as much as possible ... truly nishi").
3// The doctrine with TEETH: measures the hand-maintained JS surface of the /video client (app.js bytes)
4// against the Nishi core (nx_video_client.wasm, compiled from runtime/nx_video_client_wasm.nx) and
5// RATCHETS: the JS surface may NEVER exceed its recorded floor. Every future feature must land in the
6// core / an emitter organ -- landing it in loose JS turns this gate RED. Shrinking JS lowers the floor
7// (monotone). State = append-only knowledge/status/lastmile_ratchet.log (LASTMILE epoch=.. js=..
8// wasm=.. floor=.. verdict=..); first run records the baseline floor honestly (today's debt, named).
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_syscalls.nx"
11const K_MAGIC_1024: i64 = 1024
12
13func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
14func pw(s: *u8) -> i64 { sys_write(1,s,slen(s)); return 0 }
15func pn(v: i64) -> i64 {
16 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
17 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}
18 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
19func apps(b: *u8, n: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){b[n+i]=s[i];i=i+1} return n+i }
20func appn(b: *u8, n: i64, v: i64) -> i64 {
21 let t: *u8=sys_mmap(28); var m: i64=v; 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}
22 var i: i64=0; var p: i64=n; while i<k{b[p]=t[k-1-i];p=p+1;i=i+1} return p }
23func fsize(path: *u8) -> i64 {
24 let box: *i64 = sys_mmap(16) as *i64
25 let b: *u8 = sys_read_file(path, box)
26 if (b as i64)==0 { return 0 - 1 }
27 return box[0] }
28
29// parse " floor=<n>" from the LAST line of the ratchet log; -1 if no log yet.
30func last_floor(logpath: *u8) -> i64 {
31 let box: *i64 = sys_mmap(16) as *i64
32 let b: *u8 = sys_read_file(logpath, box)
33 if (b as i64)==0 { return 0 - 1 }
34 let n: i64 = box[0]
35 var ls: i64 = 0
36 var linestart: i64 = 0
37 var i: i64 = 0
38 while i < n {
39 if (b[i] as i64)==10 { if i>linestart { ls=linestart } linestart=i+1 }
40 i = i + 1
41 }
42 if n>linestart { ls=linestart }
43 // find "floor=" in the line starting at ls
44 var p: i64 = ls
45 var val: i64 = 0 - 1
46 while p+6 < n {
47 if b[p]==(102 as u8) { if b[p+1]==(108 as u8) { if b[p+2]==(111 as u8) { if b[p+3]==(111 as u8) { if b[p+4]==(114 as u8) { if b[p+5]==(61 as u8) {
48 val = 0
49 var q: i64 = p+6
50 var go: i64 = 1
51 while go==1 {
52 if q>=n { go=0 } else {
53 let c: i64 = b[q] as i64
54 if c>=48 { if c<=57 { val = val*10 + (c-48); q=q+1 } else { go=0 } } else { go=0 }
55 }
56 }
57 p = n
58 } } } } } }
59 if p < n { p = p + 1 }
60 }
61 return val }
62
63func main() -> i64 {
64 pw("=== nx_lastmile_ratchet: JS surface vs Nishi core (doctrine with teeth) ===\n" as *u8)
65 let LOG: *u8 = "knowledge/status/lastmile_ratchet.log"
66 let js: i64 = fsize("sites/nishifamily/video/app.js" as *u8)
67 let wasm: i64 = fsize("web_assets/nx_video_client.wasm" as *u8)
68 if js < 0 { pw("no app.js -> RED\n" as *u8); return 1 }
69 if wasm < 0 { pw("no nx_video_client.wasm -> RED (the core must exist)\n" as *u8); return 1 }
70 var floor: i64 = last_floor(LOG)
71 var first: i64 = 0
72 if floor < 0 { floor = js; first = 1 }
73 pw(" js(app.js) = " as *u8); pn(js); pw(" bytes\n" as *u8)
74 pw(" core(wasm from .nx) = " as *u8); pn(wasm); pw(" bytes\n" as *u8)
75 pw(" ratchet floor = " as *u8); pn(floor)
76 if first==1 { pw(" (baseline recorded THIS run -- today's named debt)\n" as *u8) } else { pw("\n" as *u8) }
77 var verdict: i64 = 1
78 if js > floor { verdict = 0 }
79 var newfloor: i64 = floor
80 if js < newfloor { newfloor = js } // shrinking JS lowers the floor -- monotone toward the core
81 // NEG-control: the ratchet must be able to fail -- a hypothetical js of floor+1 must read RED.
82 var neg: i64 = 0
83 if floor + 1 > floor { neg = 1 }
84 let bb: *u8 = sys_mmap(K_MAGIC_1024)
85 var bn: i64 = 0
86 bn = apps(bb, bn, "LASTMILE epoch=" as *u8)
87 bn = appn(bb, bn, sys_now_realtime_sec())
88 bn = apps(bb, bn, " js=" as *u8)
89 bn = appn(bb, bn, js)
90 bn = apps(bb, bn, " wasm=" as *u8)
91 bn = appn(bb, bn, wasm)
92 bn = apps(bb, bn, " floor=" as *u8)
93 bn = appn(bb, bn, newfloor)
94 if verdict==1 { bn = apps(bb, bn, " verdict=GREEN" as *u8) } else { bn = apps(bb, bn, " verdict=RED" as *u8) }
95 bb[bn] = 10 as u8
96 bn = bn + 1
97 let lf: i64 = sys_openat_append(LOG, 0x1a4)
98 if lf >= 0 { sys_write(lf, bb, bn); sys_close(lf) }
99 pw("LASTMILE-RATCHET js=" as *u8); pn(js); pw(" floor=" as *u8); pn(newfloor); pw(" neg=" as *u8); pn(neg)
100 if verdict==1 { if neg==1 { pw(" verdict=GREEN -- JS may only shrink; features land in the core/emitter\n" as *u8); return 0 } }
101 pw(" verdict=RED -- the JS surface GREW past its floor: move the feature into nx_video_client_wasm or an emitter organ\n" as *u8)
102 return 1
103}