code wiki / (root) / nx_leak_check_gate.nx

nx_leak_check_gate.nx source

↩ module page · 158 lines · 9864 B

1// nx_leak_check_gate.nx -- gates the leak-health certifier's REAL DATA-SCIENCE core on SYNTHETIC series 2// (deterministic, integer). The load-bearing test: a VARIABLE/jagged leak (down-up-down but trending up) is 3// CONVICTED -- the case a monotone assumption or a linear-fit R2 would MISS. Also proves honest ACQUITTAL of 4// a bounded sawtooth (variable but not leaking) and a one-time startup step (warmup-trimmed). Exact Mann- 5// Kendall S / tau / Theil-Sen values checked against hand-computed ground truth. Exit 0 on all-PASS. 6// license_tier: ORIGINAL expect_exit: 0 7import "nx_leak_check_lib.nx" 8 9const LG_CHECKS: i64 = 19 10const LG_PTR: i64 = 16 11const LG_N: i64 = 8 // samples in the generated trend series 12const LG_N4: i64 = 4 // small series for exact-value tests 13const LG_BASE: i64 = 500 // baseline kB 14const LG_TREND: i64 = 100 // per-sample rise (leak series) 15const LG_JITTER: i64 = 150 // down-jag on odd samples -> a VARIABLE (non-monotone) leak 16const LG_AMP: i64 = 400 // sawtooth / step / churn amplitude 17const LG_THREE: i64 = 3 // 3-level cycle -> high MAD (churn, median not a mode) 18const LG_SPIKE_AMP: i64 = 2000 // one-sample outlier for the SPIKE test 19const LG_BASE_FD: i64 = 10 // baseline open descriptors (fd meter test) 20const LG_FD_STEP: i64 = 3 // fd climb per sample -> 3 fd/min (an fd leak: tiny in kB terms, real in fd terms) 21const LG_CPU_HI: i64 = 800 // permille of a core: a sustained busy-loop (80% -- flat, no trend) 22const LG_CPU_LO: i64 = 50 // permille: a normal lightly-active daemon (5%) 23const LG_PAIRS: i64 = 496 24const LG_ASCII_0: i64 = 48 25const LG_SLASH: i64 = 47 26 27func lg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 28func lg_check(name: *u8, ok: i64, pass: *i64) -> i64 { 29 lg_puts("T " as *u8); lg_puts(name); lg_puts(" -> " as *u8) 30 if ok == 1 { lg_puts("PASS\n" as *u8); pass[0] = pass[0] + 1 } else { lg_puts("FAIL\n" as *u8) } 31 return 0 32} 33// uniform 1-minute time axis so Theil-Sen slope reduces to exact kB per (index-gap) minute 34func lg_fill_ts(ts: *i64, n: i64) -> i64 { var k: i64 = 0; while k < n { ts[k] = k * LC_MS_PER_MIN; k = k + 1 } return 0 } 35 36func main() -> i64 { 37 let pass: *i64 = sys_mmap(LG_PTR) as *i64 38 pass[0] = 0 39 let xs: *i64 = sys_mmap(LG_N * LG_PTR) as *i64 40 let ts: *i64 = sys_mmap(LG_N * LG_PTR) as *i64 41 let scratch: *i64 = sys_mmap(LG_PAIRS * LG_PTR) as *i64 42 let out3: *i64 = sys_mmap(LC_O_SLOTS * LG_PTR) as *i64 43 lg_fill_ts(ts, LG_N) 44 var k: i64 = 0 45 46 // T1 FLAT -> HEALTHY, tau 0 47 k = 0; while k < LG_N { xs[k] = LG_BASE; k = k + 1 } 48 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 49 var ok1: i64 = 0 50 if out3[LC_O_VERDICT] == LC_HEALTHY { if out3[LC_O_TAU] == 0 { ok1 = 1 } } 51 lg_check("flat-healthy-tau0" as *u8, ok1, pass) 52 53 // T2 CLEAN MONOTONE LEAK -> LEAK, tau +1000 (every pair rose) 54 k = 0; while k < LG_N { xs[k] = LG_BASE + k * LG_TREND; k = k + 1 } 55 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 56 var ok2: i64 = 0 57 if out3[LC_O_VERDICT] == LC_LEAK { if out3[LC_O_TAU] == LC_TAU_SCALE { ok2 = 1 } } 58 lg_check("monotone-leak-tau1000" as *u8, ok2, pass) 59 60 // T3 ★VARIABLE LEAK (down-jag every odd sample, but trending up) -> LEAK. The case linear-fit/monotone MISS. 61 k = 0; while k < LG_N { xs[k] = LG_BASE + k * LG_TREND - (k % LC_MKV_2) * LG_JITTER; k = k + 1 } 62 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 63 var ok3: i64 = 0 64 if out3[LC_O_VERDICT] == LC_LEAK { if out3[LC_O_TAU] > LC_TAU_SCALE / LC_MKV_2 { ok3 = 1 } } // tau > 500permille 65 lg_check("VARIABLE-leak-convicted" as *u8, ok3, pass) 66 67 // T4 SAWTOOTH (bounded working set, variable but NOT leaking) -> HEALTHY 68 k = 0; while k < LG_N { xs[k] = LG_BASE + (k % LC_MKV_2) * LG_AMP; k = k + 1 } 69 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 70 lg_check("sawtooth-healthy" as *u8, (out3[LC_O_VERDICT] == LC_HEALTHY) as i64, pass) 71 72 // T5 ONE-TIME STEP then flat (startup transient) -> HEALTHY (warmup drops the pre-step sample) 73 k = 0; while k < LG_N { if k == 0 { xs[k] = LG_BASE } else { xs[k] = LG_BASE + LG_AMP } k = k + 1 } 74 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 75 lg_check("step-then-flat-healthy" as *u8, (out3[LC_O_VERDICT] == LC_HEALTHY) as i64, pass) 76 77 // T6 THEIL-SEN exact: monotone step LG_TREND over uniform 1-min axis -> median slope == LG_TREND 78 k = 0; while k < LG_N4 { xs[k] = LG_BASE + k * LG_TREND; k = k + 1 } 79 lg_check("theil-sen-exact-rate" as *u8, (lc_theil_sen(xs, 0, ts, 0, LG_N4, scratch) == LG_TREND) as i64, pass) 80 81 // T7 MANN-KENDALL S exact: a monotone n=4 -> S == n(n-1)/2 == 6 (all pairs concordant-up) 82 lg_check("mk-s-exact-allpairs" as *u8, (lc_mk_s(xs, 0, LG_N4) == LG_N4 * (LG_N4 - 1) / LC_MKV_2) as i64, pass) 83 84 // T8 tau exact: S=6, n=4 -> tau == +1000 permille 85 lg_check("mk-tau-exact-1000" as *u8, (lc_mk_tau_permille(LG_N4 * (LG_N4 - 1) / LC_MKV_2, LG_N4) == LC_TAU_SCALE) as i64, pass) 86 87 // T9 SIGNIFICANCE positive: the variable-leak series' trend IS significant (95% one-sided) 88 k = 0; while k < LG_N { xs[k] = LG_BASE + k * LG_TREND - (k % LC_MKV_2) * LG_JITTER; k = k + 1 } 89 let sv: i64 = lc_mk_s(xs, LC_WARMUP, LG_N - LC_WARMUP) 90 lg_check("mk-significant-on-variable-leak" as *u8, (lc_mk_sig_up(sv, LG_N - LC_WARMUP) == 1) as i64, pass) 91 92 // T10 SIGNIFICANCE honesty: a clean but tiny 3-point rise is NOT significant (too few points) -> not sig 93 k = 0; while k < LC_MIN_N { xs[k] = LG_BASE + k * LG_TREND; k = k + 1 } 94 lg_check("mk-tiny-rise-not-significant" as *u8, (lc_mk_sig_up(lc_mk_s(xs, 0, LC_MIN_N), LC_MIN_N) == 0) as i64, pass) 95 96 // T11 ★SPIKE (reverse leak): flat with ONE big outlier (bursts up then returns) -> SPIKE, not LEAK 97 k = 0; while k < LG_N { xs[k] = LG_BASE; k = k + 1 } 98 xs[LG_N / LC_MKV_2] = LG_BASE + LG_SPIKE_AMP // outlier mid-series (past the warmup sample) 99 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 100 lg_check("spike-reverse-leak-detected" as *u8, (out3[LC_O_CODE] == LC_A_SPIKE) as i64, pass) 101 102 // T12 ★CHURN (off the racing line): a 3-level cycle -> high MAD/median, no trend, no outlier -> CHURN 103 k = 0; while k < LG_N { xs[k] = LG_BASE + (k % LG_THREE) * LG_AMP; k = k + 1 } 104 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 105 lg_check("churn-volatility-detected" as *u8, (out3[LC_O_CODE] == LC_A_CHURN) as i64, pass) 106 107 // T13 ★fd METER: a slow descriptor climb (3 fd/min) IS a LEAK under fd thresholds (multi-meter: a leak 108 // shows in whatever resource disappears -- fd catches socket/file leaks VmSize is blind to) 109 k = 0; while k < LG_N { xs[k] = LG_BASE_FD + k * LG_FD_STEP; k = k + 1 } 110 lc_classify_t(xs, 0, ts, 0, LG_N, scratch, out3, LC_FD_RATE_MIN, LC_FD_SPIKE_MIN, LC_LEVEL_OFF) 111 lg_check("fd-meter-leak-convicted" as *u8, (out3[LC_O_CODE] == LC_A_LEAK) as i64, pass) 112 113 // T14 same fd series under MEMORY thresholds -> HEALTHY (rate 3 < 64 kB/min) -- proves per-meter thresholds 114 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 115 lg_check("fd-climb-healthy-under-mem-thresholds" as *u8, (out3[LC_O_CODE] == LC_A_HEALTHY) as i64, pass) 116 117 // T15 ★CPU SUSTAINED BURN: a FLAT-high utilization series (busy-loop, NO trend) IS a LEAK via the LEVEL 118 // check -- the case my "cumulative-monotone" excuse missed: a busy-loop is high-and-flat, not trending. 119 k = 0; while k < LG_N { xs[k] = LG_CPU_HI; k = k + 1 } 120 lc_classify_t(xs, 0, ts, 0, LG_N, scratch, out3, LC_CPU_RATE_MIN, LC_CPU_SPIKE_MIN, LC_CPU_BURN_MAX) 121 lg_check("cpu-sustained-burn-leak" as *u8, (out3[LC_O_CODE] == LC_A_LEAK) as i64, pass) 122 123 // T16 CPU idle (5%, flat) -> HEALTHY (median below burn threshold, no trend) 124 k = 0; while k < LG_N { xs[k] = LG_CPU_LO; k = k + 1 } 125 lc_classify_t(xs, 0, ts, 0, LG_N, scratch, out3, LC_CPU_RATE_MIN, LC_CPU_SPIKE_MIN, LC_CPU_BURN_MAX) 126 lg_check("cpu-idle-healthy" as *u8, (out3[LC_O_CODE] == LC_A_HEALTHY) as i64, pass) 127 128 // T17 the SAME flat-high series under MEMORY thresholds (level OFF) -> HEALTHY: big-but-FLAT is not a leak 129 // (the 4GB-stable model server); the level check is opt-in (memory's "how much is too much" = baseline = R3) 130 k = 0; while k < LG_N { xs[k] = LG_CPU_HI; k = k + 1 } 131 lc_classify(xs, 0, ts, 0, LG_N, scratch, out3) 132 lg_check("high-but-flat-healthy-level-off" as *u8, (out3[LC_O_CODE] == LC_A_HEALTHY) as i64, pass) 133 134 // T18 ★SAMPLE VALIDITY: a series poisoned by a -1 (proc GONE mid-window) must be flagged invalid -- 135 // a dying organ's collapse otherwise reads as a huge negative slope / fake SPIKE (live-fleet finding 136 // 2026-07-16: exited torrent workers convicted at rate=-74M kB/min on first production contact). 137 k = 0; while k < LG_N { xs[k] = LG_BASE; k = k + 1 } 138 xs[LG_N - LC_MKV_2] = 0 - 1 139 lg_check("poisoned-row-invalid" as *u8, (lc_row_valid(xs, 0, LG_N) == 0) as i64, pass) 140 141 // T19 validity honesty: an all-valid row (zeros allowed -- fd_count can legitimately be 0) IS valid 142 k = 0; while k < LG_N { xs[k] = 0; k = k + 1 } 143 lg_check("clean-row-valid-incl-zeros" as *u8, (lc_row_valid(xs, 0, LG_N) == 1) as i64, pass) 144 145 lg_puts("LEAK-CHECK-GATE pass=" as *u8) 146 let b: *u8 = sys_mmap(LG_PTR) 147 var bo: i64 = 0 148 if pass[0] >= LC_DEC { b[bo] = (LG_ASCII_0 + pass[0] / LC_DEC) as u8; bo = bo + 1 } 149 b[bo] = (LG_ASCII_0 + pass[0] % LC_DEC) as u8; bo = bo + 1 150 b[bo] = LG_SLASH as u8; bo = bo + 1 151 if LG_CHECKS >= LC_DEC { b[bo] = (LG_ASCII_0 + LG_CHECKS / LC_DEC) as u8; bo = bo + 1 } 152 b[bo] = (LG_ASCII_0 + LG_CHECKS % LC_DEC) as u8; bo = bo + 1 153 sys_write(1, b, bo) 154 if pass[0] == LG_CHECKS { lg_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 155 lg_puts(" verdict=RED\n" as *u8) 156 sys_exit(1) 157 return 1 158}