code wiki / _hdl_build / nx_pub_selfheal_gate.nx
nx_pub_selfheal_gate.nx source
↩ module page · 68 lines · 7549 B
1// nx_pub_selfheal_gate.nx -- proves the autonomous Dr+Engineer self-heal: (1) ROOT-CAUSES each quarantined item
2// against the LIVE filesystem (grounded), (2) AUTO-FIXES an internal artifact that merely changed (re-queues it
3// with the current sha) -- BOUNDED so it can never loop, (3) does NOT auto-fix outward items (they stay gated).
4// Liar-killed: distinct causes from distinct real fs facts; the auto-resubmit is internal-only + once-per-item. license_tier: ORIGINAL expect_exit: 0
5import "nx_publisher.nx"
6import "nx_syscalls.nx"
7
8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func wn(v: i64) -> i64 { var m: i64=v; if m<0{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 }
10func dmk(p: *u8) -> i64 { __syscall(83, p as i64, 493, 0, 0, 0, 0); return 0 }
11func dun(p: *u8) -> i64 { __syscall(87, p as i64, 0, 0, 0, 0, 0); return 0 }
12func mkfile(p: *u8, c: *u8) -> i64 { let fd: i64 = sys_openat_wr(p, 0x1a4); if fd<0 { return 0 } var n: i64=0; while c[n]!=(0 as u8){n=n+1} sys_write(fd,c,n); sys_close(fd); return 1 }
13func qc(buf: *u8, o: i64, s: *u8) -> i64 { var w2: i64=o; var i: i64=0; while s[i]!=(0 as u8){buf[w2]=s[i];w2=w2+1;i=i+1} return w2 }
14func file_has(path: *u8, ndl: *u8) -> i64 {
15 let lp: *i64 = sys_mmap(8) as *i64; let d: *u8 = sys_read_file(path, lp); if (d as i64)==0 { return 0 }
16 let n: i64 = lp[0]; var nl: i64=0; while ndl[nl]!=(0 as u8){nl=nl+1}
17 var i: i64=0; while i+nl<=n { var j: i64=0; var ok: i64=1; while j<nl{ if d[i+j]!=ndl[j]{ok=0;j=nl} else {j=j+1} } if ok==1{return 1} i=i+1 } return 0
18}
19func count_lines(path: *u8) -> i64 { let lp: *i64 = sys_mmap(8) as *i64; let d: *u8 = sys_read_file(path, lp); if (d as i64)==0 { return 0 } let n: i64 = lp[0]; var c: i64=0; var i: i64=0; while i<n { if d[i]==(10 as u8){c=c+1} i=i+1 } return c }
20func dlline(buf: *u8, o: i64, reason: *u8, sha: *u8, src: *u8, dest: *u8, policy: *u8) -> i64 {
21 o=qc(buf,o,"STUCK" as *u8);buf[o]=9 as u8;o=o+1; o=qc(buf,o,reason);buf[o]=9 as u8;o=o+1; o=qc(buf,o,"0" as *u8);buf[o]=9 as u8;o=o+1
22 o=qc(buf,o,"PENDING" as *u8);buf[o]=9 as u8;o=o+1; o=qc(buf,o,"site" as *u8);buf[o]=9 as u8;o=o+1; o=qc(buf,o,"ws" as *u8);buf[o]=9 as u8;o=o+1
23 o=qc(buf,o,sha);buf[o]=9 as u8;o=o+1; o=qc(buf,o,src);buf[o]=9 as u8;o=o+1; o=qc(buf,o,dest);buf[o]=9 as u8;o=o+1; o=qc(buf,o,policy);buf[o]=10 as u8;o=o+1
24 return o
25}
26
27func main(argc: i64, argv: *i64) -> i64 {
28 w("=== nx_pub_selfheal_gate -- autonomous Dr+Engineer root-cause + bounded auto-fix ===\n" as *u8)
29 dmk("/tmp/psh" as *u8)
30 dun("/tmp/psh/dl.tsv" as *u8); dun("/tmp/psh/report.tsv" as *u8); dun("/tmp/psh/requeue.tsv" as *u8); dun("/tmp/psh/resub.tsv" as *u8); dun("/tmp/psh/led.tsv" as *u8)
31 mkfile("/tmp/psh/good.html" as *u8, "REAL-ARTIFACT\n" as *u8)
32 let rsha: *u8 = sys_mmap(72); pub_sha_file("/tmp/psh/good.html" as *u8, rsha)
33 let z: *u8 = "0000000000000000000000000000000000000000000000000000000000000000" as *u8
34 pub_record_ledger("/tmp/psh/led.tsv" as *u8, rsha, "dALREADY.html" as *u8, "site" as *u8) // good.html's CURRENT content is already live at dALREADY.html
35
36 let dl: *u8 = sys_mmap(8192); var o: i64 = 0
37 o = dlline(dl, o, "CRASH" as *u8, rsha, "/tmp/psh/good.html" as *u8, "d1.html" as *u8, "internal" as *u8) // PUBLISHER-BUG
38 o = dlline(dl, o, "FAIL" as *u8, rsha, "/tmp/psh/GONE.html" as *u8, "d2.html" as *u8, "internal" as *u8) // SRC-MISSING
39 o = dlline(dl, o, "FAIL" as *u8, z, "/tmp/psh/good.html" as *u8, "d3.html" as *u8, "internal" as *u8) // ARTIFACT-CHANGED (not-yet-live) -> AUTO-RESUBMIT
40 o = dlline(dl, o, "FAIL" as *u8, rsha, "/tmp/psh/good.html" as *u8, "d4.html" as *u8, "internal" as *u8) // DEPLOY-ERROR
41 o = dlline(dl, o, "FAIL" as *u8, z, "/tmp/psh/good.html" as *u8, "d5.html" as *u8, "outward" as *u8) // ARTIFACT-CHANGED outward -> FLAG (gated)
42 o = dlline(dl, o, "FAIL" as *u8, z, "/tmp/psh/good.html" as *u8, "dALREADY.html" as *u8, "internal" as *u8) // ALREADY-LIVE stale dup -> PRUNE (NO resubmit)
43 dl[o]=0 as u8
44 let fd: i64 = sys_openat_wr("/tmp/psh/dl.tsv" as *u8, 0x1a4); sys_write(fd, dl, o); sys_close(fd)
45
46 // FIRST heal pass (auto-resubmit enabled via requeue + bound-ledger; ledger distinguishes already-live dups)
47 let p1: i64 = pub_selfheal("/tmp/psh/dl.tsv" as *u8, "/tmp/psh/report.tsv" as *u8, "/tmp/psh/requeue.tsv" as *u8, "/tmp/psh/resub.tsv" as *u8, "/tmp/psh/led.tsv" as *u8)
48 let q1: i64 = count_lines("/tmp/psh/requeue.tsv" as *u8)
49 // SECOND heal pass over the SAME dead-letter -> the bound must prevent a second resubmit
50 let p2: i64 = pub_selfheal("/tmp/psh/dl.tsv" as *u8, "/tmp/psh/report.tsv" as *u8, "/tmp/psh/requeue.tsv" as *u8, "/tmp/psh/resub.tsv" as *u8, "/tmp/psh/led.tsv" as *u8)
51 let q2: i64 = count_lines("/tmp/psh/requeue.tsv" as *u8)
52 w(" pass1 processed="); wn(p1); w(" requeued="); wn(q1); w(" ; pass2 requeued="); wn(q2); w(" (bound: must stay "); wn(q1); w(")\n" as *u8)
53
54 var pass: i64 = 0; var tot: i64 = 0
55 tot=tot+1; if p1==6 { pass=pass+1; w(" PASS processed all 6 items\n" as *u8) } else { w(" FAIL processed != 6\n" as *u8) }
56 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "ALREADY-LIVE\tPRUNE-STALE-DUP" as *u8)==1 { pass=pass+1; w(" PASS already-live stale dup -> PRUNE (NOT resubmitted = no queue bloat)\n" as *u8) } else { w(" FAIL already-live not classified\n" as *u8) }
57 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "PUBLISHER-BUG\tFLAG-ENGINEER" as *u8)==1 { pass=pass+1; w(" PASS crash -> FLAG-ENGINEER\n" as *u8) } else { w(" FAIL crash\n" as *u8) }
58 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "SRC-MISSING\tFLAG-OWNER-RESTAGE" as *u8)==1 { pass=pass+1; w(" PASS missing src -> FLAG-OWNER-RESTAGE\n" as *u8) } else { w(" FAIL missing src\n" as *u8) }
59 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "ARTIFACT-CHANGED\tAUTO-RESUBMITTED" as *u8)==1 { pass=pass+1; w(" PASS internal changed -> AUTO-RESUBMITTED (the auto-fix)\n" as *u8) } else { w(" FAIL internal-changed not auto-fixed\n" as *u8) }
60 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "DEPLOY-ERROR\tRETRY-NEXT-PASS" as *u8)==1 { pass=pass+1; w(" PASS transient -> RETRY-NEXT-PASS\n" as *u8) } else { w(" FAIL transient\n" as *u8) }
61 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "ARTIFACT-CHANGED\tFLAG-OWNER-RESUBMIT" as *u8)==1 { pass=pass+1; w(" PASS OUTWARD changed -> FLAG (not auto-resubmitted; stays gated)\n" as *u8) } else { w(" FAIL outward auto-resubmitted (should be gated!)\n" as *u8) }
62 tot=tot+1; if q1==1 { if q2==1 { pass=pass+1; w(" PASS auto-resubmit BOUNDED: 1 re-queue, no loop on re-run\n" as *u8) } else { w(" FAIL not bounded (re-queued again on pass2)\n" as *u8) } } else { w(" FAIL pass1 did not re-queue exactly 1\n" as *u8) }
63 tot=tot+1; if file_has("/tmp/psh/report.tsv" as *u8, "ALREADY-RESUBMITTED" as *u8)==1 { pass=pass+1; w(" PASS bound recorded (ALREADY-RESUBMITTED on pass2)\n" as *u8) } else { w(" FAIL no bound marker\n" as *u8) }
64
65 w("nx_pub_selfheal_gate pass="); wn(pass); w("/"); wn(tot)
66 if pass==tot { w(" verdict=GREEN (autonomous root-cause + bounded internal auto-fix + outward stays gated)\n" as *u8); sys_exit(0); return 0 }
67 w(" verdict=RED\n" as *u8); sys_exit(1); return 1
68}