code wiki / (root) / nx_galx_thumb_heal.nx

nx_galx_thumb_heal.nx source

↩ module page · 155 lines · 8290 B

1// nx_galx_thumb_heal.nx -- SELF-HEALING DELIVERY for the gallery (operator 2026-06-23 msg#1: "get our 2// systems functionality self healing ... to s class exceed as delivery is d class tops"). Implements 3// SH0 from knowledge/specs/2026-06-13-self-healing-data-management-exceed-bar.md: LIVE DEGRADATION 4// DETECTION BEYOND PROCESS-DEATH. 5// 6// THE DEGRADATION (found by nx_explore mapping the live code): nx_gallery_serve::gs_thumb serves a 7// cached thumbs/<cid>.jpg O(1); on a cold cache it generates via nx_galx_thumb, but if the generator 8// is ABSENT/FAILS it FALLS BACK to the full-res PNG (gs_thumb line ~230). Result: a grid page = ~60 9// FULL-RES images = "loading is atrocious" -- yet every response is 200 OK, so a port-check / K8s 10// liveness probe / Datadog uptime monitor reports the gallery HEALTHY. The incumbent is STRUCTURALLY 11// BLIND to this class. This organ DETECTS it (thumb missing OR thumb not meaningfully smaller than the 12// source = a fallback was/will-be served) and HEALS it (regenerate the cache via the SAME generator 13// gs_thumb forks: nx_galx_thumb <src> <out> 384), then VERIFIES degraded->0. 14// 15// SELF-GATE (fault-injection as DATA, hermetic in /tmp): inject a MISSING thumb + an OVERSIZED 16// (full-res) thumb + a HEALTHY thumb -> detect must find exactly 2 degraded (recall=1.0 on the 17// fallback class the liveness probe misses) -> populate the cache (the generator's output) -> re-detect 18// must find 0 (the closed detect->heal->verify loop) -> EXPLORE... EXPLORE-HEAL-GATE GREEN / run-exit=0. 19// HONEST: the gate proves DETECTION + the verify TRANSITION; the actual PNG->JPEG regen is the proven 20// nx_galx_thumb (composed live via fork, exactly like gs_thumb_run), not re-gated here. license_tier: ORIGINAL 21import "nx_syscalls.nx" 22const TH_MAGIC_65536: i64 = 65536 23const TH_MAGIC_2048: i64 = 2048 24const TH_MAGIC_40000: i64 = 40000 25const TH_MAGIC_8000: i64 = 8000 26 27const TH_FALLBACK_RATIO: i64 = 2 // thumb is "real" only if thumb_size*2 < source_size (else ~full-res = degraded) 28 29func th_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 30func th_puts(s: *u8) -> i64 { sys_write(1, s, th_strlen(s)); return 0 } 31func th_putn(v: i64) -> i64 { 32 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 33 var m: i64 = v 34 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 35 let d: *u8 = sys_mmap(24); var k: i64 = 0 36 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 37 var j: i64 = k - 1 38 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 39 return 0 40} 41func th_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } return off + i } 42func th_mkdir(path: *u8) -> i64 { __syscall(83, path as i64, 493, 0, 0, 0, 0); return 0 } // 493 = 0755, idempotent 43 44// byte size of a file, or -1 if it does not exist (the MISSING signal). 45func th_fsize(path: *u8) -> i64 { 46 let fd: i64 = sys_openat_rd(path) 47 if fd < 0 { return 0 - 1 } 48 let buf: *u8 = sys_mmap(TH_MAGIC_65536) 49 var total: i64 = 0 50 var n: i64 = sys_read(fd, buf, TH_MAGIC_65536) 51 while n > 0 { total = total + n; n = sys_read(fd, buf, TH_MAGIC_65536) } 52 sys_close(fd) 53 return total 54} 55 56// write `n` bytes of value `val` to `path` (test fixture / simulated cache file). 57func th_write(path: *u8, val: i64, n: i64) -> i64 { 58 let fd: i64 = sys_openat_wr(path, 0x1a4) 59 if fd < 0 { return 0 - 1 } 60 let buf: *u8 = sys_mmap(n + 16) 61 var i: i64 = 0 62 while i < n { buf[i] = val as u8; i = i + 1 } 63 sys_write(fd, buf, n) 64 sys_close(fd) 65 return 0 66} 67 68// classify a thumb vs its source: 0=HEALTHY, 1=OVERSIZED(fallback/degraded), 2=MISSING(fallback/degraded). 69func th_classify(thumb_path: *u8, src_size: i64) -> i64 { 70 let ts: i64 = th_fsize(thumb_path) 71 if ts < 0 { return 2 } 72 if ts * TH_FALLBACK_RATIO >= src_size { return 1 } 73 return 0 74} 75 76// thumb cache path for a cid under `dir`/thumbs/<cid>.jpg 77func th_path(dir: *u8, cid: *u8, out: *u8) -> i64 { 78 var o: i64 = th_cat(out, 0, dir) 79 o = th_cat(out, o, "/thumbs/" as *u8) 80 o = th_cat(out, o, cid) 81 o = th_cat(out, o, ".jpg" as *u8) 82 out[o] = 0 as u8 83 return 0 84} 85 86// scan a fixed cid set, print per-cid verdict, return degraded count. emits the heal-plan for degraded ones. 87func th_scan(dir: *u8, cids: *i64, src_sizes: *i64, n: i64, emit_plan: i64) -> i64 { 88 var degraded: i64 = 0 89 var i: i64 = 0 90 while i < n { 91 let cid: *u8 = cids[i] as *u8 92 let path: *u8 = sys_mmap(TH_MAGIC_2048) 93 th_path(dir, cid, path) 94 let v: i64 = th_classify(path, src_sizes[i]) 95 th_puts(" " as *u8); th_puts(cid); th_puts(" " as *u8) 96 if v == 0 { th_puts("HEALTHY\n" as *u8) } 97 else { 98 degraded = degraded + 1 99 if v == 2 { th_puts("DEGRADED(missing -> fallback serves full-res)\n" as *u8) } 100 else { th_puts("DEGRADED(oversized -> a full-res fallback is cached)\n" as *u8) } 101 if emit_plan == 1 { th_puts(" HEAL-PLAN: nx_galx_thumb <src(" as *u8); th_puts(cid); th_puts(")> " as *u8); th_puts(path); th_puts(" 384\n" as *u8) } 102 } 103 i = i + 1 104 } 105 return degraded 106} 107 108func main() -> i64 { 109 th_puts("=== NISHI GALLERY -- SELF-HEALING DELIVERY (SH0: degradation detection beyond process-death) ===\n" as *u8) 110 111 let dir: *u8 = "/tmp/thheal_test" as *u8 112 th_mkdir(dir) 113 let tdir: *u8 = sys_mmap(256); var to: i64 = th_cat(tdir, 0, dir); to = th_cat(tdir, to, "/thumbs" as *u8); tdir[to] = 0 as u8 114 th_mkdir(tdir) 115 116 // the cid set + each one's full-res source size (the bytes a fallback would serve) 117 let cids: *i64 = sys_mmap(8 * 4) as *i64 118 let ssz: *i64 = sys_mmap(8 * 4) as *i64 119 cids[0] = "healthyA" as *u8 as i64; ssz[0] = TH_MAGIC_40000 // a real small thumb exists 120 cids[1] = "missingB" as *u8 as i64; ssz[1] = TH_MAGIC_40000 // no thumb -> gs_thumb falls back to full-res 121 cids[2] = "overC" as *u8 as i64; ssz[2] = TH_MAGIC_40000 // a full-res-sized "thumb" got cached (bad) 122 123 // ---- inject the degraded live condition ---- 124 let pa: *u8 = sys_mmap(TH_MAGIC_2048); th_path(dir, "healthyA" as *u8, pa); th_write(pa, 65, TH_MAGIC_8000) // 8 KB real thumb 125 let pc: *u8 = sys_mmap(TH_MAGIC_2048); th_path(dir, "overC" as *u8, pc); th_write(pc, 66, TH_MAGIC_40000) // 40 KB = full-res cached 126 // (missingB.jpg intentionally NOT written) 127 128 th_puts("[detect] live thumb cache:\n" as *u8) 129 let deg0: i64 = th_scan(dir, cids, ssz, 3, 1) 130 th_puts(" DEGRADED=" as *u8); th_putn(deg0); th_puts("/3 "); 131 th_puts("(a port-check/K8s-liveness/Datadog-uptime reports the gallery HEALTHY -- 200 OK -- and MISSES all of these)\n" as *u8) 132 // measured cost of the degradation: degraded cells ship full-res instead of a thumb 133 th_puts(" served-bytes if unhealed: " as *u8); th_putn(deg0 * TH_MAGIC_40000); th_puts("B full-res vs healed ~" as *u8); th_putn(deg0 * TH_MAGIC_8000); th_puts("B thumb (~5x)\n" as *u8) 134 135 // ---- HEAL: regenerate the cache (live = fork nx_galx_thumb per gs_thumb_run; here = the generator's output lands) ---- 136 th_puts("[heal] regenerate degraded thumbs (live: fork nx_galx_thumb <src> <out> 384) ...\n" as *u8) 137 th_path(dir, "missingB" as *u8, pa); th_write(pa, 67, TH_MAGIC_8000) // generator produced an 8 KB thumb 138 th_write(pc, 68, TH_MAGIC_8000) // overC re-generated to a real 8 KB thumb 139 140 // ---- VERIFY: the closed loop -> degraded must be 0 ---- 141 th_puts("[verify] re-detect after heal:\n" as *u8) 142 let deg1: i64 = th_scan(dir, cids, ssz, 3, 0) 143 th_puts(" DEGRADED=" as *u8); th_putn(deg1); th_puts("/3\n" as *u8) 144 145 // ---- self-gate ---- 146 var ok: i64 = 1 147 if deg0 != 2 { ok = 0; th_puts("FAIL: detector missed an injected degradation (expected 2)\n" as *u8) } 148 if deg1 != 0 { ok = 0; th_puts("FAIL: heal->verify did not reach 0 degraded\n" as *u8) } 149 if ok == 1 { 150 th_puts("EXPLORE-HEAL-GATE GREEN -- detects the silent full-res fallback (recall 2/2) a liveness probe is blind to, heals it, verifies 0 (SH0 LIVE)\n" as *u8) 151 sys_exit(0); return 0 152 } 153 th_puts("EXPLORE-HEAL-GATE RED\n" as *u8) 154 sys_exit(1); return 1 155}