code wiki / _hdl_build / nx_extract_persist_gate.nx

nx_extract_persist_gate.nx source

↩ module page · 60 lines · 3783 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_extract_persist_gate.nx -- proves DURABLE self-healing: a pattern learned in one "process" (table A) is 4// saved to disk, then a FRESH, empty table B loads it -- and the once-missed adversary URL now classifies via 5// the persisted learned pattern. This is what makes "auto-heal" real end-to-end: healing outlives the run, 6// so a site we adapted to once stays adapted after a restart. license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_media_signal.nx" 9import "nx_extract_heal.nx" 10 11func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 12" as *u8); return ok } 13func gsl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 14func cls(t: *i64, url: *u8, kb: *i64) -> i64 { return xh_classify(t, url, gsl(url), kb) } 15 16func main() -> i64 { 17 gw("extract-persist SOVEREIGN gate (learn -> save -> FRESH load -> still healed: durable self-heal)\n" as *u8) 18 var pass: i64 = 0 19 var ttl: i64 = 0 20 let kb: *i64 = sys_mmap(8) as *i64 21 let path: *u8 = "/tmp/xh_patterns.tbl" as *u8 22 let adv: *u8 = "https://cdn-x7.streamhost.net/deliver/vod/abc123" as *u8 // novel host, unseeded 23 24 // --- PROCESS A: seed, hit the adversary miss, LEARN, SAVE --- 25 let a: *i64 = xh_new() 26 xh_seed(a) 27 let seed_n: i64 = a[0] 28 let la: i64 = xh_learn(a, adv, gsl(adv), XT_DIRECT) // heal 29 let a_n: i64 = a[0] 30 let sv: i64 = xh_save(a, path) // persist 31 ttl=ttl+1; pass=pass+grow("P1 process-A learned + saved OK\x00" as *u8, ((la == 1)) as i64 * ((sv == 0) as i64)) 32 33 // --- PROCESS B: FRESH empty table, LOAD from disk (no seed call) --- 34 let b: *i64 = xh_new() 35 let loaded: i64 = xh_load(b, path) 36 ttl=ttl+1; pass=pass+grow("P2 process-B loaded ALL patterns (seed + learned)\x00" as *u8, (loaded == a_n) as i64) 37 ttl=ttl+1; pass=pass+grow("P3 loaded table size == saved table size\x00" as *u8, (b[0] == a_n) as i64) 38 39 // P4: the once-missed adversary URL now classifies in the FRESH table (healing survived the restart) 40 cls(b, adv, kb) 41 ttl=ttl+1; pass=pass+grow("P4 adversary URL classifies DIRECT after fresh load (durable heal)\x00" as *u8, (kb[0] == XT_DIRECT) as i64) 42 // P5: seed knowledge also restored (a plain .m3u8 -> HLS) 43 cls(b, "https://c/x.m3u8" as *u8, kb) 44 ttl=ttl+1; pass=pass+grow("P5 seed patterns also restored (.m3u8 -> HLS)\x00" as *u8, (kb[0] == XT_HLS) as i64) 45 // P6: a same-host sibling of the adversary also generalizes in the loaded table 46 cls(b, "https://cdn-x7.streamhost.net/deliver/live/xyz" as *u8, kb) 47 ttl=ttl+1; pass=pass+grow("P6 same-host sibling generalizes in loaded table\x00" as *u8, (kb[0] == XT_DIRECT) as i64) 48 // P7: an unrelated host is still a miss (persistence didn't corrupt discrimination) 49 cls(b, "https://videos.unrelated.net/clip" as *u8, kb) 50 ttl=ttl+1; pass=pass+grow("P7 unrelated host still NONE (no over-generalize after load)\x00" as *u8, (kb[0] == XT_NONE) as i64) 51 // P8: idempotent load -- loading again into a THIRD fresh table yields the same count (save is stable) 52 let c: *i64 = xh_new() 53 let loaded2: i64 = xh_load(c, path) 54 ttl=ttl+1; pass=pass+grow("P8 re-load stable (idempotent count)\x00" as *u8, (loaded2 == loaded) as i64) 55 56 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 57 gw("(saved " as *u8); gn(a_n); gw(" patterns; seed=" as *u8); gn(seed_n); gw(", learned=+1 -> persisted)\n" as *u8) 58 if pass == ttl { gw("verdict=GREEN (durable self-healing: a pattern learned once survives a restart via the saved table)\n" as *u8); sys_exit(0); return 0 } 59 gw("verdict=RED\n" as *u8); sys_exit(1); return 1 60}