code wiki / _hdl_build / nx_pub_metrics_gate.nx
nx_pub_metrics_gate.nx source
↩ module page · 70 lines · 5151 B
1// nx_pub_metrics_gate.nx -- R12 PROOF: DORA metrics over the ledger event-log + deploy NOTIFY channel.
2//
3// Seeds a ledger with KNOWN (outcome, pub_ts, lead): 6 events (4 success, 2 failure; one failure RECOVERS, one
4// stays unresolved), then asserts pub_dora computes the four DORA metrics EXACTLY:
5// deploys=6 succ=4 fail=2 · change-fail=333permille · span=400 freq=15/ksec · MTTR=50 (resolved=1) · avg_lead=25.
6// Liar-kills: change-fail-rate must reflect the 2 real failures (not 0/hardcoded); MTTR must pair a failure with the
7// NEXT same-dest success (the unresolved failure is NOT counted). Then pub_notify logs 2 events to a channel and the
8// content is verified. File-based (no forks). license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_publisher.nx"
11import "nx_runpath.nx"
12
13func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); 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} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
15func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" " as *u8); g_w(id); g_w(": " as *u8); if ok==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) } return 0 }
16func g_unlink(path: *u8) -> i64 { __syscall(87, path as i64, 0, 0, 0, 0, 0); return 0 }
17
18func main() -> i64 {
19 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
20 g_w("=== NX-PUB-METRICS GATE (DORA from the ledger + deploy notify channel) ===\n" as *u8)
21 let wsid: *u8 = sys_mmap(64); rp_wsid(wsid); rp_ensure(wsid)
22 let D: *u8 = sys_mmap(512); rp_path(wsid, "pmx" as *u8, D); __syscall(83, D as i64, 493, 0, 0, 0, 0)
23 let ledpath: *u8 = sys_mmap(700); pub_join(D, "ledger.tsv" as *u8, ledpath)
24 let notifypath: *u8 = sys_mmap(700); pub_join(D, "notify.tsv" as *u8, notifypath)
25 g_unlink(ledpath); g_unlink(notifypath)
26
27 // seed known events (outcome, pub_ts, lead). a.html: success100, fail300, recover350 -> MTTR 50. d.html: fail500 unresolved.
28 pub_record_outcome(ledpath, "s1" as *u8, "a.html" as *u8, "games" as *u8, "PUBLISHED" as *u8, 100, 10)
29 pub_record_outcome(ledpath, "s2" as *u8, "b.html" as *u8, "games" as *u8, "PUBLISHED" as *u8, 200, 20)
30 pub_record_outcome(ledpath, "s3" as *u8, "a.html" as *u8, "games" as *u8, "ROLLED_BACK" as *u8, 300, 0)
31 pub_record_outcome(ledpath, "s4" as *u8, "a.html" as *u8, "games" as *u8, "PUBLISHED" as *u8, 350, 30)
32 pub_record_outcome(ledpath, "s5" as *u8, "c.html" as *u8, "site" as *u8, "PUBLISHED" as *u8, 400, 40)
33 pub_record_outcome(ledpath, "s6" as *u8, "d.html" as *u8, "site" as *u8, "REJECTED" as *u8, 500, 0)
34
35 let m: *i64 = sys_mmap(128) as *i64
36 pub_dora(ledpath, m)
37
38 g_w(" DORA deploys=" as *u8); g_n(m[0]); g_w(" succ=" as *u8); g_n(m[1]); g_w(" fail=" as *u8); g_n(m[2]); g_w(" cfr_permille=" as *u8); g_n(m[3]); g_w("\n" as *u8)
39 g_w(" span=" as *u8); g_n(m[4]); g_w(" freq_per_ksec=" as *u8); g_n(m[5]); g_w(" mttr=" as *u8); g_n(m[6]); g_w(" resolved=" as *u8); g_n(m[7]); g_w(" avg_lead=" as *u8); g_n(m[8]); g_w("\n" as *u8)
40
41 let r_cnt: i64 = ((m[0]==6) as i64) & ((m[1]==4) as i64) & ((m[2]==2) as i64)
42 let r_cfr: i64 = (m[3]==333) as i64
43 let r_freq: i64 = ((m[4]==400) as i64) & ((m[5]==15) as i64)
44 let r_mttr: i64 = ((m[6]==50) as i64) & ((m[7]==1) as i64)
45 let r_lead: i64 = (m[8]==25) as i64
46
47 // NOTIFY channel
48 pub_notify(notifypath, "PUBLISHED" as *u8, "a.html" as *u8, "s1" as *u8)
49 pub_notify(notifypath, "ROLLED_BACK" as *u8, "a.html" as *u8, "s3" as *u8)
50 let np: *i64 = sys_mmap(8) as *i64
51 let nd: *u8 = sys_read_file(notifypath, np)
52 var nlines: i64 = 0
53 if (nd as i64)!=0 { var i: i64=0; while i<np[0] { if nd[i]==(10 as u8) { nlines=nlines+1 } i=i+1 } }
54 let has_rb: i64 = (pub_substr(nd, np[0], "ROLLED_BACK" as *u8)==1) as i64
55 let has_a: i64 = (pub_substr(nd, np[0], "a.html" as *u8)==1) as i64
56 let no_bogus: i64 = (pub_substr(nd, np[0], "BOGUS" as *u8)==0) as i64
57 let r_notify: i64 = ((nlines==2) as i64) & has_rb & has_a & no_bogus
58 g_w(" NOTIFY lines=" as *u8); g_n(nlines); g_w(" has_rolled_back=" as *u8); g_n(has_rb); g_w(" has_dest=" as *u8); g_n(has_a); g_w("\n" as *u8)
59
60 g_row("DORA count exact (deploys6 / succ4 / fail2)" as *u8, r_cnt, pass)
61 g_row("DORA change-fail-rate exact (333 permille; reflects 2 real failures, not hardcoded)" as *u8, r_cfr, pass)
62 g_row("DORA frequency exact (span=400s, 15 deploys/ksec)" as *u8, r_freq, pass)
63 g_row("DORA MTTR exact (50s) + LIAR-KILL: unresolved failure NOT counted (resolved=1)" as *u8, r_mttr, pass)
64 g_row("DORA avg lead-time exact (25s over successes)" as *u8, r_lead, pass)
65 g_row("NOTIFY channel: 2 events logged with correct content (and no fabricated event)" as *u8, r_notify, pass)
66
67 g_w("NX-PUB-METRICS rows=6 pass=" as *u8); g_n(pass[0])
68 if pass[0]==6 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
69 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
70}