code wiki / _hdl_build / nx_capreg_health.nx

nx_capreg_health.nx source

↩ module page · 104 lines · 4603 B

1// nx_capreg_health.nx -- the registry's self-running health organ: the manual repair this arc ran by 2// hand (crash -> restore -> dedup -> renumber) becomes a CHECK -> HEAL -> RECHECK cycle any loop can 3// fork on a schedule (Conductor pulse / gate loop / boot). RACI-clean split inside one organ: 4// ENGINEER verb = the checks (live exists; live has NO duplicate idx; allocator strictly past max; 5// journal canonical = a scratch regen introduces ZERO new remaps and matches counts) 6// DOCTOR verb = the heal on RED (cl_canonicalize -- regen the truth, never hand-edit) 7// then the checks RE-RUN: verdict reflects the POST-heal state, healed=1 records that medicine was 8// needed. Verdict line appends to knowledge/status/capreg_health.log (NTFS-durable, the same journal 9// discipline the crash proved). GREEN iff all 4 checks pass. license_tier: ORIGINAL 10import "nx_capreg_librarian.nx" 11import "nx_syscalls.nx" 12const K_MAGIC_32768: i64 = 32768 13 14// check 2: any idx appearing twice in a registry file? returns the duplicated idx, or 0 if clean 15func ch_dup_idx(path: *u8) -> i64 { 16 let lenp: *i64 = sys_mmap(16) as *i64 17 let b: *u8 = sys_read_file(path, lenp) 18 let n: i64 = lenp[0] 19 if n <= 0 { return 0 } 20 let seen: *i64 = sys_mmap(K_MAGIC_32768) as *i64 21 var ns: i64 = 0 22 var i: i64 = 0 23 while i < n { 24 let e: i64 = cl_line_end(b, i, n) 25 if cl_is_entry(b, i, n) == 1 { 26 let idx: i64 = cl_parse_int(b, i + 11, n) 27 var k: i64 = 0 28 while k < ns { if seen[k] == idx { return idx } k = k + 1 } 29 seen[ns] = idx; ns = ns + 1 30 } 31 i = e + 1 32 } 33 return 0 34} 35 36// count CAPREG entries in a file 37func ch_count(path: *u8) -> i64 { 38 let lenp: *i64 = sys_mmap(16) as *i64 39 let b: *u8 = sys_read_file(path, lenp) 40 let n: i64 = lenp[0] 41 if n <= 0 { return 0 } 42 var c: i64 = 0 43 var i: i64 = 0 44 while i < n { 45 if cl_is_entry(b, i, n) == 1 { c = c + 1 } 46 i = cl_line_end(b, i, n) + 1 47 } 48 return c 49} 50 51// the ENGINEER's check battery -> bitmask of failures (0 = all green). 52// 1=live missing/empty 2=dup idx in live 4=journal not canonical 8=live-only leak (an entry 53// reached live but not the NTFS journal -- a legacy register bypassed dual-write; one crash from loss) 54func ch_check(livepath: *u8, journalpath: *u8) -> i64 { 55 var bad: i64 = 0 56 if ch_count(livepath) <= 0 { bad = bad + 1 } 57 if ch_dup_idx(livepath) != 0 { bad = bad + 2 } 58 // canonical journal == scratch regen reproduces it 1:1 (same entry count, no dup idx) 59 let scratch: *u8 = "/tmp/_capreg_health_scratch.log" as *u8 60 let nu: i64 = cl_regen(journalpath, scratch) 61 var jbad: i64 = 0 62 if nu < 0 { jbad = 1 } 63 if jbad == 0 { if nu != ch_count(journalpath) { jbad = 1 } } 64 if jbad == 0 { if ch_dup_idx(journalpath) != 0 { jbad = 1 } } 65 if jbad == 1 { bad = bad + 4 } 66 // live-only leak tripwire: dual-write guarantees journal >= live on both count and max 67 var lbad: i64 = 0 68 if ch_count(livepath) > ch_count(journalpath) { lbad = 1 } 69 if cl_max_idx(livepath) > cl_max_idx(journalpath) { lbad = 1 } 70 if lbad == 1 { bad = bad + 8 } 71 return bad 72} 73 74// the organ: check -> heal on RED -> recheck. The Doctor's medicine ORDER matters: FIRST ingest 75// live-only entries into the journal (else canonicalize would regenerate live from the journal and 76// silently DROP legacy registrations -- the wrong-medicine hazard), THEN canonicalize. 77// out[0]=pre-heal bitmask, out[1]=healed(0/1), out[2]=post bitmask. Returns 1 iff final GREEN. 78func ch_run(livepath: *u8, journalpath: *u8, out: *i64) -> i64 { 79 out[0] = ch_check(livepath, journalpath) 80 out[1] = 0 81 if out[0] != 0 { 82 cl_ingest_live(livepath, journalpath) 83 cl_canonicalize(journalpath, livepath) 84 out[1] = 1 85 } 86 out[2] = out[0] 87 if out[1] == 1 { out[2] = ch_check(livepath, journalpath) } 88 if out[2] == 0 { return 1 } 89 return 0 90} 91 92// append the durable verdict line (the SECSTATUS/SITEQ discipline: last line = current truth) 93func ch_report(logpath: *u8, out: *i64, green: i64) -> i64 { 94 let fd: i64 = sys_openat_append(logpath, 0x1a4) 95 if fd < 0 { return 0 } 96 cl_w(fd, "CAPREGHEALTH pre=" as *u8); cl_wn(fd, out[0]) 97 cl_w(fd, " healed=" as *u8); cl_wn(fd, out[1]) 98 cl_w(fd, " post=" as *u8); cl_wn(fd, out[2]) 99 cl_w(fd, " epoch=" as *u8); cl_wn(fd, sys_now_realtime_sec()) 100 if green == 1 { cl_w(fd, " verdict=GREEN\n" as *u8) } 101 if green != 1 { cl_w(fd, " verdict=RED\n" as *u8) } 102 sys_close(fd) 103 return 1 104}