code wiki / _hdl_build / nx_tsv_migrate_gate.nx

nx_tsv_migrate_gate.nx source

↩ module page · 103 lines · 5963 B

1// nx_tsv_migrate_gate.nx -- gates the TSV->native-plane migrator on the property that actually matters: 2// the plane must REPRODUCE every data row, and the tool must refuse loudly when it cannot. T1 data-lossless 3// round-trip (all tab rows back through the real reader; the '#' row asserted ABSENT so the native 4// rows-only convention is pinned by a test rather than assumed); 5// T2 source file UNTOUCHED after migration (non-destructive by construction); T3 missing source = RED exit 1, 6// nothing committed; T4 empty/blank-only source = RED exit 1 (an empty plane is a defect, never a valid migration). 7// Runs the REAL /tmp/nx_tsv_migrate.sov.elf child inside a seeded arena. 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_store_seed_lib.nx" 10import "nx_seg_store.nx" 11import "nx_syscalls.nx" 12 13func tg_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func tg_pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 15func tg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 16func tg_wfile(path: *u8, content: *u8) -> i64 { 17 let fd: i64=sys_openat_wr(path, 0x1a4) 18 if fd<0 { return 0-1 } 19 sys_write(fd, content, tg_slen(content)) 20 sys_close(fd) 21 return 0 22} 23func tg_has(buf: *u8, n: i64, needle: *u8) -> i64 { 24 let nl: i64=tg_slen(needle) 25 if nl==0 { return 0 } 26 var i: i64=0 27 while i+nl<=n { 28 var k: i64=0 29 var ok: i64=1 30 while k<nl { if buf[i+k]!=needle[k] { ok=0; k=nl } else { k=k+1 } } 31 if ok==1 { return 1 } 32 i=i+1 33 } 34 return 0 35} 36func tg_run(dir: *u8, a1: *u8, a2: *u8) -> i64 { 37 let pid: i64=sys_fork() 38 if pid<0 { return 201 } 39 if pid==0 { 40 sys_chdir(dir) 41 let av: *i64=sys_mmap(64) as *i64 42 av[0]="/tmp/nx_tsv_migrate.sov.elf\x00" as *u8 as i64 43 av[1]=a1 as i64; av[2]=a2 as i64; av[3]=0 44 sys_execve("/tmp/nx_tsv_migrate.sov.elf\x00" as *u8, av, 0 as *i64) 45 sys_exit(127) 46 return 127 47 } 48 let stp: *i64=sys_mmap(16) as *i64 49 sys_wait4(pid, stp, 0) 50 return (stp[0]>>8)&255 51} 52 53func main() -> i64 { 54 tg_puts("=== nx_tsv_migrate_gate -- lossless round-trip + non-destructive + fail-closed ===\n" as *u8) 55 var fails: i64=0 56 sys_mkdir("/tmp/tmig\x00" as *u8, 0x1ed) 57 sys_mkdir("/tmp/tmig/knowledge\x00" as *u8, 0x1ed) 58 sys_mkdir("/tmp/tmig/knowledge/registry\x00" as *u8, 0x1ed) 59 sys_mkdir("/tmp/tmig/knowledge/store\x00" as *u8, 0x1ed) 60 // source: a comment, a blank line, and 3 tab-delimited data rows = 4 non-empty lines 61 tg_wfile("/tmp/tmig/knowledge/registry/sample.tsv\x00" as *u8, 62 "# provenance comment must survive\n\nalpha\t8\topen\nbravo\t4\trooted\ncharlie\t6\tmitigated\n\x00" as *u8) 63 let rc: i64=tg_run("/tmp/tmig\x00" as *u8, "knowledge/registry/sample.tsv\x00" as *u8, "knowledge/store/sample-\x00" as *u8) 64 // T1 round-trip: read the plane back with the REAL reader and check every payload survived 65 let bb: *u8=sys_mmap(1048576) 66 var bn: i64=0 67 let pid: i64=sys_fork() 68 if pid==0 { sys_chdir("/tmp/tmig\x00" as *u8); sys_exit(0) } 69 let stp: *i64=sys_mmap(16) as *i64 70 sys_wait4(pid, stp, 0) 71 sys_chdir("/tmp/tmig\x00" as *u8) 72 bn=sts_load("knowledge/store/sample-\x00" as *u8, bb, 1048576-4096) 73 var t1: i64=0 74 if rc==0 { if bn>0 { 75 if tg_has(bb, bn, "alpha\t8\topen\x00" as *u8)==1 { 76 if tg_has(bb, bn, "charlie\t6\tmitigated\x00" as *u8)==1 { 77 if tg_has(bb, bn, "bravo\t4\trooted\x00" as *u8)==1 { 78 // the store drops '#' rows BY DESIGN -- assert it is ABSENT so the convention is pinned, not assumed 79 if tg_has(bb, bn, "provenance comment\x00" as *u8)==0 { t1=1 } } } } 80 } } 81 if t1==1 { tg_puts("T1 PASS data-lossless round-trip: all 3 tab rows reproduced; '#' row correctly absent (native convention pinned)\n" as *u8) } else { fails=fails+1; tg_puts("T1 FAIL rc="); tg_pn(rc); tg_puts(" planebytes="); tg_pn(bn); tg_puts("\n" as *u8) } 82 // T2 non-destructive: the source file still exists with its original bytes 83 let szp: *i64=sys_mmap(16) as *i64 84 let sb: *u8=sys_read_file("/tmp/tmig/knowledge/registry/sample.tsv\x00" as *u8, szp) 85 var t2: i64=0 86 if (sb as i64)!=0 { if tg_has(sb, szp[0], "bravo\t4\trooted\x00" as *u8)==1 { t2=1 } } 87 if t2==1 { tg_puts("T2 PASS source file UNTOUCHED after migration (retiring it stays a separate reversible decision)\n" as *u8) } else { fails=fails+1; tg_puts("T2 FAIL source damaged\n" as *u8) } 88 // T3 missing source -> RED, nothing committed 89 let rc3: i64=tg_run("/tmp/tmig\x00" as *u8, "knowledge/registry/nope.tsv\x00" as *u8, "knowledge/store/nope-\x00" as *u8) 90 var t3: i64=0 91 if rc3==1 { t3=1 } 92 if t3==1 { tg_puts("T3 PASS missing source = RED exit 1 (fail-closed, no empty plane created)\n" as *u8) } else { fails=fails+1; tg_puts("T3 FAIL rc3="); tg_pn(rc3); tg_puts("\n" as *u8) } 93 // T4 blank-only source -> RED (an empty plane is a defect, not a migration) 94 tg_wfile("/tmp/tmig/knowledge/registry/blank.tsv\x00" as *u8, "\n\n\n\x00" as *u8) 95 let rc4: i64=tg_run("/tmp/tmig\x00" as *u8, "knowledge/registry/blank.tsv\x00" as *u8, "knowledge/store/blank-\x00" as *u8) 96 var t4: i64=0 97 if rc4==1 { t4=1 } 98 if t4==1 { tg_puts("T4 PASS blank-only source = RED exit 1 (an empty plane is a defect, never a valid migration)\n" as *u8) } else { fails=fails+1; tg_puts("T4 FAIL rc4="); tg_pn(rc4); tg_puts("\n" as *u8) } 99 if fails==0 { tg_puts("TSV-MIGRATE verdict=GREEN -- migration is lossless, verified by round-trip, non-destructive, and fail-closed\n" as *u8); sys_exit(0); return 0 } 100 tg_puts("TSV-MIGRATE verdict=RED fails="); tg_pn(fails); tg_puts("\n" as *u8) 101 sys_exit(1) 102 return 1 103}