code wiki / _hdl_build / _freshness_gate.nx

_freshness_gate.nx source

↩ module page · 98 lines · 4996 B

1// _freshness_gate.nx -- LOCK for the LM-027 reactive freshness detector (DISCIPLINE -> AUTO upgrade). 2// Proves the new mtime CHANNEL (sys_fstatat/262 + sys_utimensat/280, the x86_64-direct stat family) and 3// oi_src_stale: a source-vs-artifact staleness detector that works with NO /tmp twin -- so a gate that 4// compiles-then-runs an external artifact can REACTIVELY detect "artifact older than source" and refuse 5// to run yesterday's binary (the trap that printed 650 for a 125-line program), instead of only the 6// proactive oi_fresh remove-before-compile discipline. 7// 8// DETERMINISTIC: sets explicit mtimes via sys_utimensat (no sleeps / no FS-resolution races), on /tmp 9// fixtures (no _offc pollution). Self-cleaning via oi_fresh (unlinkat). 10// T1 stale : artifact mtime < source mtime -> oi_src_stale == 1 11// T2 fresh : artifact mtime > source mtime -> oi_src_stale == 0 (no false-positive) 12// T3 art-absent : artifact missing, source present -> oi_src_stale == 1 (never built) 13// T4 src-absent : source missing -> oi_src_stale == 0 (nothing to be stale vs) 14// T5 round-trip : utimensat sets mtime=S; oi_mtime_ns reads S back -> the stat is REAL (not a constant) 15// GREEN only if T1..T5 hold. Evidence -> knowledge/status/offc_install.log. license_tier: ORIGINAL 16import "nx_offc_install.nx" 17import "nx_syscalls.nx" 18 19const FR_LOG: *u8 = "knowledge/status/offc_install.log" 20const FR_SRC: *u8 = "/tmp/_frsrc" 21const FR_ART: *u8 = "/tmp/_frart" 22const FR_MISSING: *u8 = "/tmp/_frmissing_zzq" 23 24func fr_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 25func fr_p(s: *u8) -> i64 { sys_write(1, s, fr_len(s)); return 0 } 26func fr_fp(fd: i64, s: *u8) -> i64 { sys_write(fd, s, fr_len(s)); return 0 } 27func fr_write(path: *u8, content: *u8) -> i64 { 28 let fd: i64 = sys_openat_wr(path, 420) 29 if fd < 0 { return 0 } 30 sys_write(fd, content, fr_len(content)); sys_close(fd) 31 return 1 32} 33// set path atime+mtime to `sec` seconds (nsec 0) via utimensat -- the deterministic clock for the test. 34func fr_set_mtime(path: *u8, sec: i64) -> i64 { 35 let ts: *i64 = sys_mmap(64) as *i64 36 ts[0] = sec; ts[1] = 0; ts[2] = sec; ts[3] = 0 37 return sys_utimensat(path, ts) 38} 39func fr_row(name: *u8, pass: i64) -> i64 { fr_p(" " as *u8); fr_p(name); if pass == 1 { fr_p(" PASS\n" as *u8) } else { fr_p(" FAIL\n" as *u8) } return 0 } 40 41func main() -> i64 { 42 oi_fresh(FR_MISSING) // guarantee the missing path is absent 43 fr_write(FR_SRC, "source-content-v1" as *u8) 44 fr_write(FR_ART, "artifact-content-v1" as *u8) 45 46 // T1 stale: artifact older than source 47 fr_set_mtime(FR_ART, 1700000000) 48 fr_set_mtime(FR_SRC, 1700000100) 49 var t1: i64 = 0 50 if oi_src_stale(FR_ART, FR_SRC) == 1 { t1 = 1 } 51 52 // T2 fresh: artifact newer than source (no false-positive) 53 fr_set_mtime(FR_ART, 1700000200) 54 var t2: i64 = 0 55 if oi_src_stale(FR_ART, FR_SRC) == 0 { t2 = 1 } 56 57 // T3 artifact-absent: source exists, artifact gone -> stale 58 var t3: i64 = 0 59 if oi_src_stale(FR_MISSING, FR_SRC) == 1 { t3 = 1 } 60 61 // T4 source-absent: nothing to be stale against -> not stale 62 var t4: i64 = 0 63 if oi_src_stale(FR_ART, FR_MISSING) == 0 { t4 = 1 } 64 65 // T5 mtime round-trip: utimensat sets it, fstatat reads it back (the stat is REAL, not a constant) 66 fr_set_mtime(FR_ART, 1700000042) 67 var t5: i64 = 0 68 if oi_mtime_ns(FR_ART) / 1000000000 == 1700000042 { t5 = 1 } 69 70 oi_fresh(FR_SRC); oi_fresh(FR_ART) // cleanup 71 72 var passes: i64 = 0 73 if t1 == 1 { passes = passes + 1 } 74 if t2 == 1 { passes = passes + 1 } 75 if t3 == 1 { passes = passes + 1 } 76 if t4 == 1 { passes = passes + 1 } 77 if t5 == 1 { passes = passes + 1 } 78 var ok: i64 = 0 79 if passes == 5 { ok = 1 } 80 81 fr_p("FRESHNESS gate (LM-027 reactive source-vs-artifact mtime detector)\n" as *u8) 82 fr_row("T1 detect-stale " as *u8, t1) 83 fr_row("T2 neg-control-fresh " as *u8, t2) 84 fr_row("T3 artifact-absent " as *u8, t3) 85 fr_row("T4 source-absent " as *u8, t4) 86 fr_row("T5 mtime-round-trip " as *u8, t5) 87 88 let lf: i64 = sys_openat_append(FR_LOG, 420) 89 if ok == 1 { 90 fr_p("FRESHGATE verdict=GREEN keystone=lm027-reactive-freshness probe=src-artifact-mtime stat=newfstatat-262 touch=utimensat-280 detect+neg+absent+roundtrip all-pass\n" as *u8) 91 if lf >= 0 { fr_fp(lf, "FRESHGATE verdict=GREEN keystone=lm027-reactive-freshness probe=src-artifact-mtime detect+neg+absent+roundtrip all-pass epoch=" as *u8); oi_wn(lf, sys_now_realtime_sec()); fr_fp(lf, "\n" as *u8); sys_close(lf) } 92 sys_exit(0); return 0 93 } 94 fr_p("FRESHGATE verdict=RED (mtime detector not fully proven)\n" as *u8) 95 if lf >= 0 { fr_fp(lf, "FRESHGATE verdict=RED passes=" as *u8); oi_wn(lf, passes); fr_fp(lf, "\n" as *u8); sys_close(lf) } 96 sys_exit(1) 97 return 1 98}