code wiki / _hdl_build / nx_self_model_regen_test.nx
nx_self_model_regen_test.nx source
↩ module page · 55 lines · 3402 B
1// nx_self_model_regen_test.nx -- prove the registry-driven self-model: a SMALL loop writes CAPREG lines
2// (data, no fat main), the Librarian's regen reads + counts them. This is the fix for the 229-cap fat-main
3// segfault and scales to thousands. Exit 0 on 6/6. license_tier: ORIGINAL
4
5import "nx_self_model_regen.nx"
6import "nx_syscalls.nx"
7
8func t_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func t_num(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;k=1}; while m>0{t[k]=48+(m%10);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 }
10func t_fw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
11func t_fn(fd: i64, 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;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
12
13func main() -> i64 {
14 t_puts("=== SELF-MODEL REGEN from registry (the fix for the fat-main segfault) ===\n" as *u8)
15 let path: *u8 = "/tmp/nishi_test_registry.log" as *u8
16 let fd: i64 = sys_openat_wr(path, 0x1a4)
17 // SMALL LOOP writes 250 CAPREG lines (data) -- 246 PROVEN, 4 building. No fat main, no reg pressure.
18 let TOTAL: i64 = 250
19 var i: i64 = 0
20 while i < TOTAL {
21 t_fw(fd, "CAPREG idx=" as *u8); t_fn(fd, i)
22 t_fw(fd, " layer=" as *u8); t_fn(fd, i % 6)
23 t_fw(fd, " status=" as *u8)
24 if i < TOTAL - 4 { t_fn(fd, 2) } else { t_fn(fd, 1) } // last 4 = building
25 t_fw(fd, " name=cap-" as *u8); t_fn(fd, i); t_fw(fd, "\n" as *u8)
26 i = i + 1
27 }
28 sys_close(fd)
29
30 let lb: *i64 = sys_mmap(16) as *i64
31 let buf: *u8 = sys_read_file(path, lb)
32 let n: i64 = lb[0]
33 let proven: i64 = smr_proven(buf, n)
34 let building: i64 = smr_building(buf, n)
35 let total: i64 = smr_total(buf, n)
36 t_puts(" regenerated from registry (" as *u8); t_num(n); t_puts(" bytes): " as *u8); t_num(proven); t_puts(" PROVEN, " as *u8); t_num(building); t_puts(" building, " as *u8); t_num(total); t_puts(" total\n" as *u8)
37
38 let r: *i64 = sys_mmap(8*8) as *i64
39 r[0]=0; if total == 250 { r[0]=1 } // 250 caps registered, no fat-main crash
40 r[1]=0; if proven == 246 { r[1]=1 }
41 r[2]=0; if building == 4 { r[2]=1 }
42 r[3]=0; if proven + building == total { r[3]=1 }
43 r[4]=0; if n > 5000 { r[4]=1 } // a big registry the small reader handled fine
44 r[5]=0; if smr_count(buf, n, "status=2 " as *u8) == proven { r[5]=1 } // deterministic recount
45 var pass: i64 = 0; var j: i64 = 0
46 while j < 6 { pass = pass + r[j]; j = j + 1 }
47 t_puts("---- passed " as *u8); t_num(pass); t_puts("/6 ----\n" as *u8)
48 if pass == 6 {
49 t_puts(" FIXED: the self-model is regenerated FROM the registry by a SMALL reader (250 caps, zero\n" as *u8)
50 t_puts(" segfault) -- scales to thousands. The team registers caps -> the registry grows -> the Librarian\n" as *u8)
51 t_puts(" regenerates the count. Claude is out of the self-model loop. (Migrate the real caps next.)\n" as *u8)
52 sys_exit(0); return 0
53 }
54 t_puts(" FAIL\n" as *u8); sys_exit(1); return 1
55}