code wiki / (root) / nx_torrent_health.nx

nx_torrent_health.nx source

↩ module page · 308 lines · 17866 B

1// nx_torrent_health.nx -- S-CLASS FUNCTIONAL HEALTH MONITOR for /torrent (the "s-class exceed" 2// functionality monitor). Where nx_torrent_live_probe only proves the PAGE loads, and 3// nx_torrent_doctor only reads LOCAL download status, THIS exercises the REAL magnet pipeline 4// end-to-end against a known-good canary (Big Buck Bunny, DHT-only -- the operator's trackerless 5// use case) and reports a per-stage GREEN/RED LADDER that LOCALIZES any break: 6// [1] PARSE mg_parse decodes the magnet info_hash (pure) 7// [2] BUNDLE every spawn-sibling worker elf is present in self-dir -- catches a tmpfs-wiped 8// bundle after a NAS reboot OR an incomplete deploy ("magnets AND other stuff dead") 9// [3] DISCOVERY the real nx_torrent_get worker finds peers>0 (catches NAS egress / DHT block) 10// [4] METADATA BEP-9 ut_metadata resolves the torrent size>0 11// [5] PIECE >=1 sha1-verified piece downloads (catches "peers connected but no data served") 12// It RUNS THE REAL WORKER (DRY -- it tests the exact binary users use, never a reimplementation), 13// polls download.status, then kills the worker's whole process GROUP + cleans the scratch dir. 14// `--selftest` is a DETERMINISTIC logic gate (synthetic status fixtures + neg-controls), so the 15// grader/localizer/parser are proven GREEN with no network; the live canary is the functional proof. 16// 17// usage: nx_torrent_health [--selftest | <magnet>] (default live canary = Big Buck Bunny) 18// license_tier: ORIGINAL 19// module: nishi-core.torrent.health 20// depends: nishi-core.torrent.magnet, nishi-core.sys.syscalls 21import "nx_syscalls.nx" 22import "nx_magnet.nx" 23const HS_MAGIC_1024: i64 = 1024 24const HS_MAGIC_8192: i64 = 8192 25const HS_MAGIC_2000: i64 = 2000 26const HS_MAGIC_8191: i64 = 8191 27const HS_MAGIC_276561920: i64 = 276561920 28const HS_MAGIC_1055: i64 = 1055 29 30const HS_BBB: *u8 = "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny" as *u8 31const HS_SCRATCH: *u8 = "/tmp/_nx_thealth" as *u8 32const HS_MAXPOLL: i64 = 24 // 24 * 2s = 48s ceiling for the live canary 33 34// ---------------- tiny io ---------------- 35func hs_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 36func hs_wn(v: i64) -> i64 { 37 let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(1,"-" as *u8,1)} 38 var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} 39 let b: *u8=sys_mmap(28); var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 40} 41func hs_whexb(b: *u8, len: i64) -> i64 { 42 let hd: *u8 = "0123456789abcdef" as *u8; var i: i64=0 43 while i<len { let v: i64=b[i] as i64; let o: *u8=sys_mmap(2); o[0]=hd[(v>>4)&0xf]; o[1]=hd[v&0xf]; sys_write(1,o,2); i=i+1 } 44 return 0 45} 46func hs_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 47func hs_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 } 48func hs_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 49func hs_starts(s: *u8, pre: *u8) -> i64 { var i: i64=0; while pre[i]!=(0 as u8){ if s[i]!=pre[i]{return 0} i=i+1 } return 1 } 50// build "<dir>/<name>" into a fresh NUL-terminated buffer 51func hs_join(dir: *u8, name: *u8) -> *u8 { let p: *u8 = sys_mmap(HS_MAGIC_1024); var o: i64 = hs_cat(p,0,dir); p[o]=47 as u8; o=o+1; o = hs_cat(p,o,name); p[o]=0 as u8; return p } 52 53// ---------------- fs helpers ---------------- 54func hs_file_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd>=0 { sys_close(fd); return 1 } return 0 } 55func hs_unlink(path: *u8) -> i64 { return __syscall(263, AT_FDCWD, path as i64, 0, 0, 0, 0) } // unlinkat(AT_FDCWD,path,0) 56func hs_rmdir(path: *u8) -> i64 { return __syscall(263, AT_FDCWD, path as i64, 0x200, 0, 0, 0) } // unlinkat(...,AT_REMOVEDIR) 57// read a whole file into buf (bounded); returns bytes read or 0. 58func hs_read(path: *u8, buf: *u8, cap: i64) -> i64 { 59 let fd: i64 = sys_openat_rd(path); if fd<0 { return 0 } 60 var off: i64=0; var go: i64=1 61 while go==1 { if off>=cap { go=0 } else { let r: i64=sys_read(fd, ((buf as i64)+off) as *u8, cap-off); if r<=0 {go=0} else {off=off+r} } } 62 sys_close(fd); return off 63} 64 65// ---------------- the self-dir (mirrors daemon d_self_dir) ---------------- 66// The dir THIS binary lives in, read from /proc/self/cmdline argv[0]. The spawn-sibling worker elfs 67// resolve here on BOTH the dev box (/tmp) and the NAS hub (/volume1/ai/torrent). Fallback "/tmp". 68func hs_self_dir() -> *u8 { 69 let buf: *u8 = sys_mmap(HS_MAGIC_1024) 70 let fd: i64 = sys_openat_rd("/proc/self/cmdline" as *u8) 71 var n: i64 = 0 72 if fd >= 0 { n = sys_read(fd, buf, 1023); sys_close(fd) } 73 if n <= 0 { let fb: *u8 = sys_mmap(8); fb[0]=47 as u8; fb[1]=116 as u8; fb[2]=109 as u8; fb[3]=112 as u8; fb[4]=0 as u8; return fb } 74 var z: i64 = 0; var dz: i64 = 0 75 while dz == 0 { if z >= n { dz = 1 } else { if buf[z] == (0 as u8) { dz = 1 } else { z = z + 1 } } } 76 var i: i64 = z - 1; var df: i64 = 0 77 while df == 0 { if i <= 0 { df = 1 } else { if buf[i] == (47 as u8) { buf[i] = 0 as u8; df = 1 } else { i = i - 1 } } } 78 if buf[0] == (0 as u8) { let fb: *u8 = sys_mmap(8); fb[0]=47 as u8; fb[1]=116 as u8; fb[2]=109 as u8; fb[3]=112 as u8; fb[4]=0 as u8; return fb } 79 return buf 80} 81 82// ---------------- pure grading + status parsing ---------------- 83// status is newline-delimited key=value; return the int value of `key` (matched at a LINE START), or -1. 84func hs_status_int(buf: *u8, n: i64, key: *u8) -> i64 { 85 let kl: i64 = hs_len(key) 86 var i: i64 = 0 87 while i < n { 88 var ls: i64 = 0; if i==0 { ls=1 } else { if (buf[i-1] as i64)==10 { ls=1 } } 89 if ls==1 { 90 var m: i64=1; var j: i64=0 91 while j<kl { if i+j>=n { m=0; j=kl } else { if buf[i+j]!=key[j] { m=0; j=kl } else { j=j+1 } } } 92 if m==1 { 93 var p: i64=i+kl; var v: i64=0; var any: i64=0; var go: i64=1 94 while go==1 { if p>=n { go=0 } else { let c: i64=buf[p] as i64; if c>=48 { if c<=57 { v=v*10+(c-48); p=p+1; any=1 } else { go=0 } } else { go=0 } } } 95 if any==1 { return v } 96 return 0 97 } 98 } 99 i=i+1 100 } 101 return 0 - 1 102} 103func hs_grade(v: i64) -> i64 { if v>0 { return 1 } return 0 } // a stage is GREEN iff its counter > 0 104// the FIRST failing stage (1..5), or 0 if all GREEN -- this is the localization 105func hs_first_fail(parse: i64, bundle: i64, disc: i64, meta: i64, piece: i64) -> i64 { 106 if parse==0 { return 1 } 107 if bundle==0 { return 2 } 108 if disc==0 { return 3 } 109 if meta==0 { return 4 } 110 if piece==0 { return 5 } 111 return 0 112} 113func hs_stage_name(s: i64) -> i64 { 114 if s==1 { hs_w("PARSE (magnet malformed)" as *u8) } 115 if s==2 { hs_w("BUNDLE (worker bundle incomplete -- redeploy as daemon siblings; /tmp is NOEXEC on the NAS)" as *u8) } 116 if s==3 { hs_w("DISCOVERY (no peers -- check NAS outbound UDP/DHT + tracker reachability/firewall)" as *u8) } 117 if s==4 { hs_w("METADATA (BEP-9 ut_metadata never resolved)" as *u8) } 118 if s==5 { hs_w("PIECE (peers + metadata but no data served)" as *u8) } 119 return 0 120} 121 122// ---------------- [2] BUNDLE: every spawn-sibling worker present in self-dir ---------------- 123// Returns 1 iff the CRITICAL worker (nx_torrent_get) is present. Prints its own ladder line + any 124// missing siblings. nx_torrent_get absent => no downloads at all (the tmpfs-reboot failure mode). 125func hs_bundle(selfdir: *u8) -> i64 { 126 let names: *i64 = sys_mmap(8*8) as *i64 127 let crit: *i64 = sys_mmap(8*8) as *i64 128 names[0]=("nx_torrent_get.sov.elf" as *u8) as i64; crit[0]=1 129 names[1]=("nx_torrent_extract.sov.elf" as *u8) as i64; crit[1]=0 130 names[2]=("nx_magnet_discover.sov.elf" as *u8) as i64; crit[2]=0 131 names[3]=("nx_video_get.sov.elf" as *u8) as i64; crit[3]=0 132 names[4]=("nx_hls_get.sov.elf" as *u8) as i64; crit[4]=0 133 names[5]=("nx_galx_vidindex.sov.elf" as *u8) as i64; crit[5]=0 134 let cnt: i64 = 6 135 var present: i64 = 0; var crit_ok: i64 = 1 136 var i: i64 = 0 137 while i < cnt { 138 let pth: *u8 = hs_join(selfdir, names[i] as *u8) 139 if hs_file_exists(pth)==1 { present=present+1 } else { if crit[i]==1 { crit_ok=0 } } 140 i=i+1 141 } 142 hs_w("[2] BUNDLE " as *u8) 143 if crit_ok==1 { hs_w("GREEN " as *u8) } else { hs_w("RED " as *u8) } 144 hs_w("present " as *u8); hs_wn(present); hs_w("/" as *u8); hs_wn(cnt); hs_w(" workers\n" as *u8) 145 if present < cnt { 146 i=0 147 while i<cnt { 148 let pth: *u8 = hs_join(selfdir, names[i] as *u8) 149 if hs_file_exists(pth)==0 { hs_w(" MISSING: " as *u8); hs_w(names[i] as *u8); if crit[i]==1 { hs_w(" (CRITICAL -- no downloads without it)" as *u8) } hs_w("\n" as *u8) } 150 i=i+1 151 } 152 } 153 return crit_ok 154} 155 156// ---------------- live canary: spawn the REAL worker, poll status ---------------- 157// Fills out[0..3] = peers,total,have,pieces (running max over the poll window). Spawns the worker in 158// its OWN process group (setpgid) so the whole swarm can be killed; silences worker stdout/stderr. 159func hs_canary(selfdir: *u8, magnet: *u8, out: *i64) -> i64 { 160 out[0]=0; out[1]=0; out[2]=0; out[3]=0 161 sys_mkdir(HS_SCRATCH, 0x1ff) 162 let worker: *u8 = hs_join(selfdir, "nx_torrent_get.sov.elf" as *u8) 163 if hs_file_exists(worker)==0 { return 0 } 164 let argv: *i64 = sys_mmap(64) as *i64 165 argv[0]=worker as i64; argv[1]=magnet as i64; argv[2]=HS_SCRATCH as i64; argv[3]=0 166 let envp: *i64 = sys_mmap(16) as *i64; envp[0]=("PATH=/usr/bin:/bin" as *u8) as i64; envp[1]=0 167 let devnull: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 168 let pid: i64 = sys_fork() 169 if pid < 0 { return 0 } 170 if pid == 0 { 171 __syscall(109, 0, 0, 0, 0, 0, 0) // setpgid(0,0): own group -> killable swarm 172 if devnull>=0 { sys_dup3(devnull,1,0); sys_dup3(devnull,2,0) } 173 sys_execve(worker, argv, envp) 174 sys_exit(127) 175 } 176 if devnull>=0 { sys_close(devnull) } 177 let statp: *u8 = hs_join(HS_SCRATCH, "download.status" as *u8) 178 let buf: *u8 = sys_mmap(HS_MAGIC_8192) 179 var t: i64 = 0 180 while t < HS_MAXPOLL { 181 sys_sleep_ms(HS_MAGIC_2000); t=t+1 182 let n: i64 = hs_read(statp, buf, HS_MAGIC_8191) 183 if n>0 { 184 let pe: i64 = hs_status_int(buf,n,"peers=" as *u8); if pe>out[0] { out[0]=pe } 185 let to: i64 = hs_status_int(buf,n,"total=" as *u8); if to>out[1] { out[1]=to } 186 let hv: i64 = hs_status_int(buf,n,"have=" as *u8); if hv>out[2] { out[2]=hv } 187 let pc: i64 = hs_status_int(buf,n,"pieces=" as *u8); if pc>out[3] { out[3]=pc } 188 if out[2]>0 { t=HS_MAXPOLL } // a verified piece -> full path proven, stop early 189 } 190 } 191 nx_kill(0 - pid, 9) // kill the whole swarm (group) 192 let st: *i64 = sys_mmap(16) as *i64 193 sys_wait4(pid, st, 0) 194 var rg: i64 = 1 195 while rg==1 { let w: i64 = sys_wait4(0-1, st, WNOHANG); if w<=0 { rg=0 } } 196 return 1 197} 198 199func hs_cleanup() -> i64 { 200 let sfx: *i64 = sys_mmap(8*16) as *i64 201 sfx[0]=("download.part" as *u8) as i64 202 sfx[1]=("download.status" as *u8) as i64 203 sfx[2]=("download.done" as *u8) as i64 204 sfx[3]=("download.npc" as *u8) as i64 205 sfx[4]=("download.meta" as *u8) as i64 206 sfx[5]=("download.control" as *u8) as i64 207 sfx[6]=("download.avail" as *u8) as i64 208 sfx[7]=("download.inflight" as *u8) as i64 209 sfx[8]=("download.stream" as *u8) as i64 210 sfx[9]=("download.pex" as *u8) as i64 211 let cnt: i64 = 10 212 var i: i64 = 0 213 while i<cnt { hs_unlink(hs_join(HS_SCRATCH, sfx[i] as *u8)); i=i+1 } 214 hs_rmdir(HS_SCRATCH) 215 return 0 216} 217 218// ---------------- live mode: the full ladder ---------------- 219func hs_live(magnet: *u8) -> i64 { 220 let selfdir: *u8 = hs_self_dir() 221 hs_w("=== NISHI TORRENT FUNCTIONAL HEALTH (s-class) ===\n" as *u8) 222 hs_w("self-dir=" as *u8); hs_w(selfdir); hs_w("\ncanary=" as *u8); hs_w(magnet); hs_w("\n" as *u8) 223 224 // [1] PARSE 225 let ihash: *u8 = sys_mmap(32); let trk: *i64 = sys_mmap(8*MG_NTRK_MAX) as *i64; let ntrk: *i64 = sys_mmap(16) as *i64; let nm: *u8 = sys_mmap(HS_MAGIC_1024) 226 var parse: i64 = 0 227 if mg_parse(magnet, ihash, trk, MG_NTRK_MAX, ntrk, nm)==1 { parse=1 } 228 hs_w("[1] PARSE " as *u8) 229 if parse==1 { hs_w("GREEN info_hash=" as *u8); hs_whexb(ihash,4); hs_w("..\n" as *u8) } else { hs_w("RED not a valid btih magnet\n" as *u8) } 230 231 // [2] BUNDLE 232 var bundle: i64 = 0 233 if parse==1 { bundle = hs_bundle(selfdir) } else { hs_w("[2] BUNDLE SKIP (parse failed)\n" as *u8) } 234 235 // [3][4][5] via the real worker canary 236 var disc: i64 = 0; var meta: i64 = 0; var piece: i64 = 0 237 let out: *i64 = sys_mmap(8*8) as *i64; out[0]=0; out[1]=0; out[2]=0; out[3]=0 238 if bundle==1 { 239 hs_w(" running real worker canary (bounded " as *u8); hs_wn(HS_MAXPOLL*2); hs_w("s)...\n" as *u8) 240 hs_canary(selfdir, magnet, out) 241 disc = hs_grade(out[0]); meta = hs_grade(out[1]); piece = hs_grade(out[2]) 242 hs_cleanup() 243 } else { 244 hs_w(" (skipping live canary -- critical worker missing)\n" as *u8) 245 } 246 hs_w("[3] DISCOVERY " as *u8) 247 if disc==1 { hs_w("GREEN peers=" as *u8); hs_wn(out[0]); hs_w("\n" as *u8) } else { hs_w("RED 0 peers (NAS egress/DHT blocked? trackers down?)\n" as *u8) } 248 hs_w("[4] METADATA " as *u8) 249 if meta==1 { hs_w("GREEN size=" as *u8); hs_wn(out[1]); hs_w(" bytes (BEP-9 ut_metadata)\n" as *u8) } else { hs_w("RED unresolved (no peer served ut_metadata)\n" as *u8) } 250 hs_w("[5] PIECE " as *u8) 251 if piece==1 { hs_w("GREEN have=" as *u8); hs_wn(out[2]); hs_w("/" as *u8); hs_wn(out[3]); hs_w(" sha1-verified\n" as *u8) } else { hs_w("RED no verified piece downloaded\n" as *u8) } 252 253 let ff: i64 = hs_first_fail(parse, bundle, disc, meta, piece) 254 if ff==0 { hs_w("--- VERDICT: GREEN full magnet pipeline LIVE (parse->bundle->peers->metadata->piece) ---\n" as *u8); return 0 } 255 hs_w("--- VERDICT: RED at stage " as *u8); hs_wn(ff); hs_w(" -- " as *u8); hs_stage_name(ff); hs_w(" ---\n" as *u8) 256 return ff 257} 258 259// ---------------- --selftest: deterministic logic gate (no network) ---------------- 260func hs_selftest() -> i64 { 261 hs_w("=== NISHI TORRENT HEALTH -- SELFTEST (deterministic logic gate) ===\n" as *u8) 262 var ok: i64 = 1 263 // grader: GREEN iff > 0, and -1 (key-not-found) reads as RED 264 if hs_grade(80)!=1 { ok=0; hs_w(" FAIL grade(80)\n" as *u8) } 265 if hs_grade(1)!=1 { ok=0; hs_w(" FAIL grade(1)\n" as *u8) } 266 if hs_grade(0)!=0 { ok=0; hs_w(" FAIL grade(0)\n" as *u8) } 267 if hs_grade(0-1)!=0 { ok=0; hs_w(" FAIL grade(-1)\n" as *u8) } 268 // localizer: first failing stage 269 if hs_first_fail(0,0,0,0,0)!=1 { ok=0; hs_w(" FAIL ff=parse\n" as *u8) } 270 if hs_first_fail(1,0,0,0,0)!=2 { ok=0; hs_w(" FAIL ff=bundle\n" as *u8) } 271 if hs_first_fail(1,1,0,0,0)!=3 { ok=0; hs_w(" FAIL ff=disc\n" as *u8) } 272 if hs_first_fail(1,1,1,0,0)!=4 { ok=0; hs_w(" FAIL ff=meta\n" as *u8) } 273 if hs_first_fail(1,1,1,1,0)!=5 { ok=0; hs_w(" FAIL ff=piece\n" as *u8) } 274 if hs_first_fail(1,1,1,1,1)!=0 { ok=0; hs_w(" FAIL ff=allgreen\n" as *u8) } 275 // status parse round-trip via a REAL fixture file 276 sys_mkdir(HS_SCRATCH, 0x1ff) 277 let fx: *u8 = hs_join(HS_SCRATCH, "download.status" as *u8) 278 let fixture: *u8 = "total=276561920\ndone=1048576\nhave=3\npieces=1055\npeers=80\nstate=downloading\nname=Big Buck Bunny\n" as *u8 279 let fd: i64 = sys_openat_wr(fx, 0x1a4); if fd>=0 { sys_write(fd, fixture, hs_len(fixture)); sys_close(fd) } 280 let buf: *u8 = sys_mmap(HS_MAGIC_8192); let n: i64 = hs_read(fx, buf, HS_MAGIC_8191) 281 if hs_status_int(buf,n,"peers=" as *u8)!=80 { ok=0; hs_w(" FAIL parse peers\n" as *u8) } 282 if hs_status_int(buf,n,"total=" as *u8)!=HS_MAGIC_276561920 { ok=0; hs_w(" FAIL parse total\n" as *u8) } 283 if hs_status_int(buf,n,"have=" as *u8)!=3 { ok=0; hs_w(" FAIL parse have\n" as *u8) } 284 if hs_status_int(buf,n,"pieces=" as *u8)!=HS_MAGIC_1055 { ok=0; hs_w(" FAIL parse pieces\n" as *u8) } 285 if hs_status_int(buf,n,"bogus=" as *u8)!=(0-1) { ok=0; hs_w(" FAIL parse missing-key\n" as *u8) } 286 // neg-control: a no-peers status grades to DISCOVERY red -> localized at stage 3 287 if hs_first_fail(1,1,hs_grade(0),hs_grade(0-1),hs_grade(0-1))!=3 { ok=0; hs_w(" FAIL nopeers->stage3\n" as *u8) } 288 // file-exists logic 289 if hs_file_exists(fx)!=1 { ok=0; hs_w(" FAIL exists(fixture)\n" as *u8) } 290 if hs_file_exists("/tmp/_nx_thealth/__nope__" as *u8)!=0 { ok=0; hs_w(" FAIL exists(bogus)\n" as *u8) } 291 // PARSE on the real BBB canary magnet decodes the info_hash 292 let ih: *u8 = sys_mmap(32); let tk: *i64 = sys_mmap(8*MG_NTRK_MAX) as *i64; let nt: *i64 = sys_mmap(16) as *i64; let na: *u8 = sys_mmap(HS_MAGIC_1024) 293 if mg_parse(HS_BBB, ih, tk, MG_NTRK_MAX, nt, na)!=1 { ok=0; hs_w(" FAIL parse BBB canary\n" as *u8) } 294 if (ih[0] as i64)!=221 { ok=0; hs_w(" FAIL BBB ihash[0]\n" as *u8) } // 0xdd 295 // cleanup 296 hs_unlink(fx); hs_rmdir(HS_SCRATCH) 297 if ok==1 { hs_w("SELFTEST verdict=GREEN (grader + localizer + status-parse + exists all correct)\n" as *u8); sys_exit(0); return 0 } 298 hs_w("SELFTEST verdict=RED\n" as *u8); sys_exit(1); return 1 299} 300 301func main(argc: i64, argv: *i64) -> i64 { 302 if argc>=2 { 303 let a1: *u8 = argv[1] as *u8 304 if hs_streq(a1, "--selftest" as *u8)==1 { return hs_selftest() } 305 if hs_starts(a1, "magnet:" as *u8)==1 { let r: i64 = hs_live(a1); sys_exit(r); return r } 306 } 307 let r: i64 = hs_live(HS_BBB); sys_exit(r); return r 308}