code wiki / _hdl_build / nx_hostop_state_gate.nx
nx_hostop_state_gate.nx source
↩ module page · 49 lines · 4216 B
1// nx_hostop_state_gate.nx -- R8 PROOF: host state mgmt -- drift detection, dry-run plan, idempotent apply.
2// SANDBOX hosts: h1 matches desired (no drift), h2 differs (drift), h3 missing (drift). File-based. license_tier: ORIGINAL expect_exit: 0
3import "nx_syscalls.nx"
4import "nx_hostop.nx"
5import "nx_runpath.nx"
6
7func 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 }
8func 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 }
9func 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 }
10func sv_write(path: *u8, content: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd<0 { return 0 } var n: i64=0; while content[n]!=(0 as u8){n=n+1} sys_write(fd, content, n); sys_close(fd); return 1 }
11func g_unlink(path: *u8) -> i64 { __syscall(87, path as i64, 0, 0, 0, 0, 0); return 0 }
12func g_mkdir(D: *u8, name: *u8) -> *u8 { let p: *u8 = sys_mmap(700); pub_join(D, name, p); __syscall(83, p as i64, 493, 0, 0, 0, 0); return p }
13
14func main() -> i64 {
15 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
16 g_w("=== NX-HOSTOP-STATE GATE (drift / dry-run plan / idempotent apply) ===\n" as *u8)
17 let wsid: *u8 = sys_mmap(64); rp_wsid(wsid); rp_ensure(wsid)
18 let D: *u8 = sys_mmap(512); rp_path(wsid, "hst" as *u8, D); __syscall(83, D as i64, 493, 0, 0, 0, 0)
19 let live: *u8 = g_mkdir(D, "pub_live" as *u8)
20 let led: *u8 = sys_mmap(700); pub_join(D, "ledger.tsv" as *u8, led); g_unlink(led)
21 let lp: *u8 = sys_mmap(700); pub_join(live, "page.html" as *u8, lp); sv_write(lp, "DESIRED-V1\n" as *u8)
22 let sha: *u8 = sys_mmap(72); pub_sha_file(lp, sha)
23 pub_record_ledger(led, sha, "page.html" as *u8, "site" as *u8)
24
25 let h1: *u8 = g_mkdir(D, "h1" as *u8); let h2: *u8 = g_mkdir(D, "h2" as *u8); let h3: *u8 = g_mkdir(D, "h3" as *u8)
26 let h1p: *u8 = sys_mmap(700); pub_join(h1, "page.html" as *u8, h1p); sv_write(h1p, "DESIRED-V1\n" as *u8) // matches
27 let h2p: *u8 = sys_mmap(700); pub_join(h2, "page.html" as *u8, h2p); sv_write(h2p, "OLD-V0\n" as *u8) // drifted
28 let h3p: *u8 = sys_mmap(700); pub_join(h3, "page.html" as *u8, h3p); g_unlink(h3p) // missing
29
30 let d1: i64 = hostop_drift(h1, "page.html" as *u8, sha)
31 let d2: i64 = hostop_drift(h2, "page.html" as *u8, sha)
32 let d3: i64 = hostop_drift(h3, "page.html" as *u8, sha)
33 let hosts: *i64 = sys_mmap(64) as *i64; hosts[0]=h1 as i64; hosts[1]=h2 as i64; hosts[2]=h3 as i64
34 let plan: i64 = hostop_dryrun(hosts, 3, "page.html" as *u8, sha)
35
36 let i_noop: i64 = hostop_idempotent(led, live, h1, "page.html" as *u8, sha) // h1 matches -> no-op (2)
37 let i_apply: i64 = hostop_idempotent(led, live, h2, "page.html" as *u8, sha) // h2 drifted -> apply (1)
38 let i_again: i64 = hostop_idempotent(led, live, h2, "page.html" as *u8, sha) // h2 now matches -> no-op (2)
39
40 g_w(" drift: h1(match)=" as *u8); g_n(d1); g_w(" h2(diff)=" as *u8); g_n(d2); g_w(" h3(missing)=" as *u8); g_n(d3); g_w(" dryrun_would_change=" as *u8); g_n(plan); g_w("\n" as *u8)
41 g_w(" idempotent: h1=" as *u8); g_n(i_noop); g_w(" (2=noop) h2=" as *u8); g_n(i_apply); g_w(" (1=applied) h2again=" as *u8); g_n(i_again); g_w(" (2=noop)\n" as *u8)
42 g_row("DRIFT detects matching(0) vs drifted/missing(1)" as *u8, ((d1==0) as i64) & ((d2==1) as i64) & ((d3==1) as i64), pass)
43 g_row("DRY-RUN plans the would-change count (2 of 3) and applies NOTHING" as *u8, ((plan==2) as i64) & ((pub_exists(h3p)==0) as i64), pass)
44 g_row("IDEMPOTENT: no-op on matching host, applies only the drifted one, re-apply is no-op" as *u8, ((i_noop==2) as i64) & ((i_apply==1) as i64) & ((i_again==2) as i64), pass)
45
46 g_w("NX-HOSTOP-STATE rows=3 pass=" as *u8); g_n(pass[0])
47 if pass[0]==3 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
48 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
49}