code wiki / _hdl_build / nx_algo_retire.nx

nx_algo_retire.nx source

↩ module page · 98 lines · 5165 B

1// nx_algo_retire.nx -- the DEBT-DROP: retire the 3,281 specs/algo_registry/<name>/meta.json now that the cards 2// live byte-faithful in knowledge/store/algo-registry- + the sovereign registry (nx_algo_registry) preserves the 3// capability + the only consumer (nx_bundle_gen) is dormant. SAFE BY CONSTRUCTION: each meta.json is moved ONLY 4// IF its bytes BYTE-MATCH the store copy (no data loss -- it's in the store AND the moved file), and it is MOVED 5// (sys_renameat) to specs/algo_registry/_retired/ (the auditor fences "/_retired/" -> un-counted) -- NEVER DELETED 6// (rule #13 additive, never-brick: reversible by moving back). impl.nx/test.nx (the primitives) are untouched. 7// license_tier: ORIGINAL 8import "nx_seg_store.nx" 9import "nx_syscalls.nx" 10const AR_MAGIC_262144: i64 = 262144 11const AR_MAGIC_1024: i64 = 1024 12const AR_MAGIC_3000: i64 = 3000 13 14const AR_PREFIX: *u8 = "knowledge/store/algo-registry-" as *u8 15 16func rt_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 17func rt_putn(v: i64) -> i64 { 18 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 19 var m: i64 = v 20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 21 let d: *u8 = sys_mmap(24); var k: i64 = 0 22 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 23 var j: i64 = k - 1 24 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 25 return 0 26} 27func rt_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 28 if an != bn { return 0 } 29 var i: i64 = 0; while i < an { if a[i] != b[i] { return 0 } i = i + 1 } 30 return 1 31} 32func rt_isdot(nm: *u8) -> i64 { 33 if nm[0] == (46 as u8) { 34 if nm[1] == (0 as u8) { return 1 } 35 if nm[1] == (46 as u8) { if nm[2] == (0 as u8) { return 1 } } 36 } 37 // skip the _retired sink dir itself 38 if nm[0] == (95 as u8) { if nm[1] == (114 as u8) { if nm[2] == (101 as u8) { return 1 } } } // "_re..." 39 return 0 40} 41func main() -> i64 { 42 rt_puts("ALGO RETIRE: move byte-verified meta.json -> specs/algo_registry/_retired/ (reversible; debt-drop)\n" as *u8) 43 sys_mkdir("specs/algo_registry/_retired" as *u8, 0x1ed) 44 let h: *i64 = ss_open(AR_PREFIX) 45 if (h as i64) == 0 { rt_puts(" store open FAILED -- abort (never retire without the store source)\n" as *u8); return 1 } 46 let pp: *i64 = sys_mmap(16) as *i64 47 let ll: *i64 = sys_mmap(16) as *i64 48 let szp: *i64 = sys_mmap(16) as *i64 49 let fd: i64 = sys_openat_rd("specs/algo_registry" as *u8) 50 if fd < 0 { rt_puts(" open registry FAILED\n" as *u8); return 1 } 51 let dbuf: *u8 = sys_mmap(AR_MAGIC_262144) 52 var retired: i64 = 0 53 var unverified: i64 = 0 54 var nometa: i64 = 0 55 var go: i64 = 1 56 while go == 1 { 57 let nb: i64 = sys_getdents64(fd, dbuf, AR_MAGIC_262144) 58 if nb <= 0 { go = 0 } else { 59 var off: i64 = 0 60 while off < nb { 61 let rec: *u8 = (dbuf as i64 + off) as *u8 62 let rl: i64 = dirent_reclen(rec) 63 if rl <= 0 { off = nb } else { 64 let nm: *u8 = dirent_name(rec) 65 if rt_isdot(nm) == 0 { 66 // metapath = specs/algo_registry/<nm>/meta.json 67 let mp: *u8 = sys_mmap(AR_MAGIC_1024); var o: i64 = 0 68 o = ss_cat(mp, o, "specs/algo_registry/" as *u8) 69 o = ss_cat(mp, o, nm); o = ss_cat(mp, o, "/meta.json" as *u8); mp[o] = 0 as u8 70 szp[0] = 0 71 let fb: *u8 = ss_readall(mp, szp) 72 let flen: i64 = szp[0] 73 if flen <= 0 { nometa = nometa + 1 } else { 74 // store copy by card name (= dir name) 75 if ss_hget(h, nm, pp, ll) == 1 { 76 if rt_eq(fb, flen, pp[0] as *u8, ll[0]) == 1 { 77 // byte-verified in the store -> MOVE to _retired (never delete) 78 let np: *u8 = sys_mmap(AR_MAGIC_1024); var o2: i64 = 0 79 o2 = ss_cat(np, o2, "specs/algo_registry/_retired/" as *u8) 80 o2 = ss_cat(np, o2, nm); o2 = ss_cat(np, o2, ".json" as *u8); np[o2] = 0 as u8 81 if sys_renameat(mp, np) == 0 { retired = retired + 1 } else { unverified = unverified + 1 } 82 } else { unverified = unverified + 1 } 83 } else { unverified = unverified + 1 } 84 } 85 } 86 off = off + rl 87 } 88 } 89 } 90 } 91 sys_close(fd) 92 rt_puts(" retired(moved)="); rt_putn(retired) 93 rt_puts(" unverified(kept)="); rt_putn(unverified) 94 rt_puts(" no-meta(kept)="); rt_putn(nometa); rt_puts("\n" as *u8) 95 if retired >= AR_MAGIC_3000 { if unverified == 0 { rt_puts("ALGO-RETIRE: GREEN (all cards byte-verified-in-store + moved to _retired; reversible; the JSON debt is dropped)\n" as *u8); return 0 } } 96 rt_puts("ALGO-RETIRE: partial (kept any unverified -- safe)\n" as *u8) 97 return 0 98}