nx_galx_monitor.nx source
↩ module page · 146 lines · 9428 B
1// nx_galx_monitor.nx -- SOVEREIGN media CONSISTENCY monitor (operator 2026-06-23: "the system still acts
2// odd ... new thumbnails in the old sort ... this type of stuff is the s class exceeds monitor see and
3// catch we want to get"). The s-class exceed = catch CORRECTNESS/CONSISTENCY drift that a liveness/uptime
4// probe is structurally BLIND to (SH0). Reads the LIVE NAS (mounted /mnt/nas_ai) + checks invariants;
5// each violation is CAUGHT + named. No sh; runs from WSL over the mount. license_tier: ORIGINAL
6//
7// C1 VIDEO-CATALOG-CLEAN no #recycle (trash) paths in galx_vid_paths.tsv
8// C2 VIDEO-OFFSET-FRESH galx_vid_off.bin tsv_size == actual tsv size (else per-request full scan = slow)
9// C3 THUMB-COVERAGE deep (unbiased) sample of thumbs present (cold = full-res fallback = slow grid)
10// C4 IMAGE-INDEX-FRESH newest image date-dir on disk is reflected in the index (else new gens sort stale)
11import "nx_syscalls.nx"
12const MON_MAGIC_524288: i64 = 524288
13const MON_MAGIC_65536: i64 = 65536
14const MON_MAGIC_1048576: i64 = 1048576
15
16const MON_BIG: i64 = 8388608 // 8MB read window (vid_paths ~4.3MB)
17const GX: *u8 = "/mnt/nas_ai/galx"
18const VIDTSV: *u8 = "/mnt/nas_ai/galx/knowledge/status/galx_vid_paths.tsv"
19const VIDOFF: *u8 = "/mnt/nas_ai/galx/knowledge/status/galx_vid_off.bin"
20const CIDTSV: *u8 = "/mnt/nas_ai/galx/knowledge/status/galx_cid_paths.tsv"
21const IMGDIR: *u8 = "/mnt/nas_ai/images"
22
23func mp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
24func mn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let d: *u8=sys_mmap(24); var k: i64=0; while m>0{d[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=k-1; while i>=0{let o: *u8=sys_mmap(1);o[0]=d[i];sys_write(1,o,1);i=i-1} return 0 }
25func mcat(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 }
26func mfind(buf: *u8, lo: i64, hi: i64, pat: *u8, pl: i64) -> i64 { if pl==0{return 0-1} var i: i64=lo; while i+pl<=hi { var j: i64=0; while j<pl { if buf[i+j]!=pat[j]{j=pl+1}else{j=j+1} } if j==pl{return i} i=i+1 } return 0-1 }
27func mexists(path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0{return 0} sys_close(fd); return 1 }
28func mslen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
29
30func main() -> i64 {
31 mp("=== NISHI MEDIA CONSISTENCY MONITOR (sovereign; SH0 = catch correctness drift a liveness probe misses) ===\n" as *u8)
32 var anomalies: i64 = 0
33 let buf: *u8 = sys_mmap(MON_BIG + 16)
34
35 // ---- C1 VIDEO-CATALOG-CLEAN + the tsv size for C2 ----
36 let fd1: i64 = sys_openat_rd(VIDTSV)
37 var vsz: i64 = 0
38 if fd1 < 0 { mp("C1 VIDEO-CATALOG ANOMALY cannot read vid catalog\n" as *u8); anomalies = anomalies + 1 }
39 else {
40 var n: i64 = sys_read(fd1, buf, MON_BIG); sys_close(fd1)
41 if n < 0 { n = 0 }
42 vsz = n
43 var lines: i64 = 0; var rec: i64 = 0; var i: i64 = 0; var ls: i64 = 0
44 while i < n {
45 if buf[i]==(10 as u8) {
46 lines = lines + 1
47 if mfind(buf, ls, i, "#recycle" as *u8, 8) >= 0 { rec = rec + 1 }
48 ls = i + 1
49 }
50 i = i + 1
51 }
52 if rec > 0 { mp("C1 VIDEO-CATALOG ANOMALY " as *u8); mn(rec); mp(" of " as *u8); mn(lines); mp(" entries point at #recycle TRASH (videos 404)\n" as *u8); anomalies = anomalies + 1 }
53 else { mp("C1 VIDEO-CATALOG OK " as *u8); mn(lines); mp(" live recordings, 0 #recycle\n" as *u8) }
54 }
55
56 // ---- C2 VIDEO-OFFSET-FRESH ----
57 let fo: i64 = sys_openat_rd(VIDOFF)
58 if fo < 0 { mp("C2 VIDEO-OFFSET ANOMALY galx_vid_off.bin missing -> per-request full scan = slow\n" as *u8); anomalies = anomalies + 1 }
59 else {
60 let hb: *u8 = sys_mmap(32); let hr: i64 = sys_read(fo, hb, 16); sys_close(fo)
61 var stored: i64 = 0
62 if hr == 16 { var k: i64 = 0; while k < 8 { stored = (stored << 8) | (hb[k] as i64); k = k + 1 } }
63 if stored != vsz { mp("C2 VIDEO-OFFSET ANOMALY offset stale (stored=" as *u8); mn(stored); mp(" actual=" as *u8); mn(vsz); mp(") -> O(n) scan per /vid = slow grid\n" as *u8); anomalies = anomalies + 1 }
64 else { mp("C2 VIDEO-OFFSET OK fresh (tsv_size=" as *u8); mn(stored); mp(")\n" as *u8) }
65 }
66
67 // ---- C3 THUMB-COVERAGE (DEEP unbiased sample, not the prewarmed head) ----
68 let fc: i64 = sys_openat_rd(CIDTSV)
69 if fc < 0 { mp("C3 THUMB-COVERAGE ANOMALY cannot read image index\n" as *u8); anomalies = anomalies + 1 }
70 else {
71 let csz: i64 = sys_lseek(fc, 0, 2) // actual size
72 var soff: i64 = csz / 2; if soff < 0 { soff = 0 } // middle = un-prewarmed (not the head)
73 sys_lseek(fc, soff, 0)
74 var n: i64 = sys_read(fc, buf, MON_MAGIC_524288); sys_close(fc)
75 if n < 0 { n = 0 }
76 var tot: i64 = 0; var have: i64 = 0; var i: i64 = 0; var ls: i64 = 0; var started: i64 = 0
77 while i <= n {
78 var eol: i64 = 0; if i==n {eol=1} else { if buf[i]==(10 as u8){eol=1} }
79 if eol == 1 {
80 if started == 1 { if tot < 40 {
81 // cid = field before the first tab
82 var tab: i64 = i; var t: i64 = ls; while t < i { if buf[t]==(9 as u8){tab=t;t=i} else {t=t+1} }
83 let cl: i64 = tab - ls
84 if cl >= 60 { if cl <= 75 {
85 let p: *u8 = sys_mmap(512); var o: i64 = mcat(p, 0, GX); o = mcat(p, o, "/thumbs/" as *u8)
86 var q: i64 = ls; while q < tab { p[o]=buf[q]; o=o+1; q=q+1 }
87 o = mcat(p, o, ".jpg" as *u8); p[o]=0 as u8
88 tot = tot + 1
89 if mexists(p) == 1 { have = have + 1 }
90 } }
91 }
92 started = 1 } // skip the first (partial) line after the seek
93 ls = i + 1
94 }
95 i = i + 1
96 }
97 var pc: i64 = 0; if tot > 0 { pc = (have * 100) / tot }
98 if tot == 0 { mp("C3 THUMB-COVERAGE WARN deep sample empty (index < 20MB?)\n" as *u8) }
99 else { if pc < 50 { mp("C3 THUMB-COVERAGE ANOMALY deep sample " as *u8); mn(have); mp("/" as *u8); mn(tot); mp(" (" as *u8); mn(pc); mp("%) -> cold cells fall back to full-res = slow grid (prewarm incomplete)\n" as *u8); anomalies = anomalies + 1 }
100 else { mp("C3 THUMB-COVERAGE OK deep sample " as *u8); mn(have); mp("/" as *u8); mn(tot); mp(" (" as *u8); mn(pc); mp("%)\n" as *u8) } }
101 }
102
103 // ---- C4 IMAGE-INDEX-FRESH: newest image date-dir present in the index tail (else new gens sort stale) ----
104 let dfd: i64 = sys_openat_rd(IMGDIR)
105 if dfd < 0 { mp("C4 IMAGE-INDEX WARN cannot open " as *u8); mp(IMGDIR); mp("\n" as *u8) }
106 else {
107 let db: *u8 = sys_mmap(MON_MAGIC_65536); let maxd: *u8 = sys_mmap(64); maxd[0]=0 as u8
108 var go: i64 = 1
109 while go == 1 {
110 let nr: i64 = sys_getdents64(dfd, db, MON_MAGIC_65536)
111 if nr <= 0 { go = 0 } else {
112 var off: i64 = 0
113 while off < nr {
114 let rec: *u8 = (db as i64 + off) as *u8
115 let rl: i64 = dirent_reclen(rec); let nm: *u8 = dirent_name(rec)
116 // date dirs look like 2026-..; pick the lexically-greatest (= newest) starting with '2'
117 if nm[0]==(50 as u8) {
118 var cmp: i64 = 0; var a: i64 = 0; var done: i64 = 0
119 while done == 0 { let x: i64 = nm[a] as i64; let y: i64 = maxd[a] as i64; if x>y {cmp=1;done=1} else { if x<y {cmp=0-1;done=1} else { if x==0 {done=1} else {a=a+1} } } }
120 if cmp >= 0 { var c: i64 = 0; while nm[c]!=(0 as u8){maxd[c]=nm[c];c=c+1} maxd[c]=0 as u8 }
121 }
122 if rl <= 0 { off = nr } else { off = off + rl }
123 }
124 }
125 }
126 sys_close(dfd)
127 if maxd[0]==(0 as u8) { mp("C4 IMAGE-INDEX WARN no date dirs found\n" as *u8) }
128 else {
129 // read the index TAIL (last 1MB) and look for "/images/<maxdate>/"
130 let fc2: i64 = sys_openat_rd(CIDTSV)
131 if fc2 < 0 { mp("C4 IMAGE-INDEX WARN cannot read index\n" as *u8) }
132 else {
133 let end: i64 = sys_lseek(fc2, 0, 2); var st: i64 = end - MON_MAGIC_1048576; if st < 0 { st = 0 }
134 sys_lseek(fc2, st, 0)
135 let tn: i64 = sys_read(fc2, buf, MON_MAGIC_1048576); sys_close(fc2)
136 let pat: *u8 = sys_mmap(96); var po: i64 = mcat(pat, 0, "/images/" as *u8)
137 var z: i64 = 0; while maxd[z]!=(0 as u8){pat[po]=maxd[z];po=po+1;z=z+1} pat[po]=47 as u8; po=po+1; pat[po]=0 as u8
138 if mfind(buf, 0, tn, pat, mslen(pat)) >= 0 { mp("C4 IMAGE-INDEX OK newest date-dir " as *u8); mp(maxd); mp(" is in the index tail (sort fresh)\n" as *u8) }
139 else { mp("C4 IMAGE-INDEX ANOMALY newest date-dir " as *u8); mp(maxd); mp(" NOT in the index tail -> new generations are stale-sorted / not reflected newest-first\n" as *u8); anomalies = anomalies + 1 }
140 }
141 }
142 }
143
144 mp("MONITOR ANOMALIES-CAUGHT=" as *u8); mn(anomalies); mp(" (>0 = the system is acting odd; each line above names what + why)\n" as *u8)
145 sys_exit(0); return 0
146}