code wiki / (root) / nx_gauge_gate.nx

nx_gauge_gate.nx source

↩ module page · 111 lines · 6734 B

1// nx_gauge_gate.nx -- CE9 (codeeffectiveness sm_gauge_alarm): the gauge-heartbeat ruler proven on planted stamps. 2// In-process over nx_gauge_lib (the ONE classifier the writing beat and the /compare emitter both use), so a stale 3// gauge is proven to read STALE and never zero, a blind axis to read BLIND, an absent stamp ABSENT, a future stamp 4// STALE (clock skew is never fresh), the value present ONLY on a FRESH row, and an unpinned cadence STALE by 5// construction. Every parsed field is -1 when absent, never 0. Rows written with real bytes under /tmp. 6// exit: 0 GREEN . 1 RED . 3 SKIP license_tier: ORIGINAL No hw writes. 7import "nx_syscalls.nx" 8import "nx_gate_verdict.nx" 9import "nx_gauge_lib.nx" 10 11const GG_ROOT: *u8 = "/tmp/nx_gauge_gate" 12const GG_FRESH: *u8 = "/tmp/nx_gauge_gate/fresh.stamp" 13const GG_STALE: *u8 = "/tmp/nx_gauge_gate/stale.stamp" 14const GG_FUTURE: *u8 = "/tmp/nx_gauge_gate/future.stamp" 15const GG_BLIND: *u8 = "/tmp/nx_gauge_gate/blind.stamp" 16const GG_NOCAD: *u8 = "/tmp/nx_gauge_gate/nocad.stamp" 17const GG_NOFIELD: *u8 = "/tmp/nx_gauge_gate/nofield.stamp" 18const GG_ABSENT: *u8 = "/tmp/nx_gauge_gate/does-not-exist.stamp" 19const GG_MODE_RWX: i64 = 493 20const GG_MODE_RW: i64 = 420 21const GG_NOW: i64 = 1788700000 22const GG_CAD: i64 = 100 // max_age = 2*100 = 200 23const GG_GAUGE: i64 = 42 24const GG_DOC: i64 = 4096 25 26func gg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 27func gg_contains(buf: *u8, n: i64, needle: *u8) -> i64 { 28 let m: i64 = gg_slen(needle) 29 if m == 0 { return 0 } 30 var i: i64 = 0 31 while i + m <= n { var j: i64 = 0; var ok: i64 = 1; while j < m { if buf[i + j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 } 32 return 0 33} 34// write a raw stamp line verbatim (for the missing-field fixtures the writer would never emit) 35func gg_raw(path: *u8, s: *u8) -> i64 { 36 sys_unlinkat(path) 37 let fd: i64 = sys_openat_wr(path, GG_MODE_RW) 38 if fd < 0 { return 0 - 1 } 39 let wr: i64 = sys_write(fd, s, gg_slen(s)) 40 sys_close(fd) 41 return wr 42} 43 44func main(argc: i64, argv: *i64) -> i64 { 45 let ctr: *i64 = gv_ctr() 46 gv_head("NX-GAUGE-GATE: a gauge heartbeat reads STALE not zero, BLIND on an abstaining axis, ABSENT with no stamp, and shows its value only when FRESH" as *u8) 47 sys_mkdir(GG_ROOT, GG_MODE_RWX) 48 sys_unlinkat(GG_ABSENT) 49 // planted stamps through the ONE writer 50 let w1: i64 = ga_stamp_write(GG_FRESH, GG_NOW - 50, GG_GAUGE, 2, 2, GG_CAD) 51 let w2: i64 = ga_stamp_write(GG_STALE, GG_NOW - 500, GG_GAUGE, 2, 2, GG_CAD) 52 let w3: i64 = ga_stamp_write(GG_FUTURE, GG_NOW + 300, GG_GAUGE, 2, 2, GG_CAD) 53 let w4: i64 = ga_stamp_write(GG_BLIND, GG_NOW - 50, GG_GAUGE, 1, 2, GG_CAD) 54 // raw fixtures the writer would never emit: no cadence field, and no gauge field 55 gg_raw(GG_NOCAD, "ts=1788699950 gauge=42 axes_known=2 axes_total=2\n" as *u8) 56 gg_raw(GG_NOFIELD, "ts=1788699950 axes_known=2 axes_total=2 cadence_s=100\n" as *u8) 57 if gv_need("planted stamps written under /tmp" as *u8, ((w1 > 0) as i64) * ((w4 > 0) as i64), ctr) == 0 { return gv_verdict("nx_gauge_gate" as *u8, ctr, "fixture tree unwritable" as *u8) } 58 59 let f: *i64 = sys_mmap(GA_F_SLOTS * GA_WORD) as *i64 60 // ---- derived bar ---- 61 gv_check_eq("max-age-is-two-beats-of-the-cadence" as *u8, ga_max_age(GG_CAD), 200, ctr) 62 // ---- FRESH round-trips and reads its value ---- 63 let sf: i64 = ga_judge(GG_FRESH, GG_NOW, GG_CAD, f) 64 gv_check_eq("fresh-stamp-value-round-trips-exactly" as *u8, f[GA_F_GAUGE], GG_GAUGE, ctr) 65 gv_check_eq("fresh-inside-two-beats-is-FRESH" as *u8, sf, GA_FRESH, ctr) 66 // ---- STALE beyond two beats ---- 67 let ss: i64 = ga_judge(GG_STALE, GG_NOW, GG_CAD, f) 68 gv_check_eq("beyond-two-beats-is-STALE" as *u8, ss, GA_STALE, ctr) 69 // ---- future stamp (clock skew) is never fresh ---- 70 let su: i64 = ga_judge(GG_FUTURE, GG_NOW, GG_CAD, f) 71 gv_check_eq("future-stamp-clock-skew-is-STALE-never-fresh" as *u8, su, GA_STALE, ctr) 72 // ---- BLIND: an axis abstained ---- 73 let sb: i64 = ga_judge(GG_BLIND, GG_NOW, GG_CAD, f) 74 gv_check_eq("axis-abstained-inside-window-is-BLIND" as *u8, sb, GA_BLIND, ctr) 75 // ---- ABSENT: no stamp at all ---- 76 let sa: i64 = ga_judge(GG_ABSENT, GG_NOW, GG_CAD, f) 77 gv_check_eq("no-stamp-is-ABSENT" as *u8, sa, GA_ABSENT, ctr) 78 // ---- an unpinned cadence judges STALE by construction (cadence_s=0 takes it from the stamp, which has none) ---- 79 let sc: i64 = ga_judge(GG_NOCAD, GG_NOW, 0, f) 80 gv_check_eq("stamp-without-a-pinned-cadence-is-STALE-by-construction" as *u8, sc, GA_STALE, ctr) 81 gv_check_eq("absent-cadence-field-reads-negative-one-never-zero" as *u8, f[GA_F_CADENCE], 0 - 1, ctr) 82 // ---- an absent gauge FIELD reads -1, never 0 ---- 83 ga_judge(GG_NOFIELD, GG_NOW, GG_CAD, f) 84 gv_check_eq("absent-gauge-field-reads-negative-one-never-zero" as *u8, f[GA_F_GAUGE], 0 - 1, ctr) 85 // ---- the renderer shows the value ONLY when FRESH ---- 86 let d: *u8 = sys_mmap(GG_DOC) 87 ga_judge(GG_FRESH, GG_NOW, GG_CAD, f) 88 let lf: i64 = ga_render(d, 0, GA_FRESH, GG_NOW - (GG_NOW - 50), 200, f[GA_F_GAUGE], f[GA_F_KNOWN], f[GA_F_TOTAL]) 89 let fresh_shows: i64 = gg_contains(d, lf, "gauge=42" as *u8) 90 gv_check("render-FRESH-shows-the-value" as *u8, fresh_shows, ctr) 91 let ds: *u8 = sys_mmap(GG_DOC) 92 let ls: i64 = ga_render(ds, 0, GA_STALE, 500, 200, GG_GAUGE, 2, 2) 93 let stale_shows: i64 = gg_contains(ds, ls, "gauge=" as *u8) 94 gv_check("render-STALE-withholds-the-value" as *u8, (stale_shows == 0) as i64, ctr) 95 gv_check("render-STALE-names-itself" as *u8, gg_contains(ds, ls, "STALE" as *u8), ctr) 96 // ---- BITE: the value-withholding fires on a stale gauge and is silent on a fresh one ---- 97 // (bad = a stale gauge, where the detector correctly withholds; good = a fresh gauge, where it shows) 98 gv_bite("withhold-value-fires-on-STALE-silent-on-FRESH" as *u8, (stale_shows == 0) as i64, (fresh_shows == 0) as i64, ctr) 99 100 gv_values_head() 101 gv_kv("fresh_state" as *u8, sf) 102 gv_kv("stale_state" as *u8, ss) 103 gv_kv("future_state" as *u8, su) 104 gv_kv("blind_state" as *u8, sb) 105 gv_kv("absent_state" as *u8, sa) 106 gv_kv("nocad_state" as *u8, sc) 107 gv_kv("max_age_of_100" as *u8, ga_max_age(GG_CAD)) 108 gv_kv("fresh_render_shows_value" as *u8, fresh_shows) 109 gv_kv("stale_render_shows_value" as *u8, stale_shows) 110 return gv_verdict("nx_gauge_gate" as *u8, ctr, "the gauge ruler reproduces FRESH/STALE/BLIND/ABSENT on planted stamps, withholds the value on every non-fresh state, treats a future stamp and an unpinned cadence as STALE, and reads every absent field as -1 not 0" as *u8) 111}