code wiki / _hdl_build / nx_autorun_poke.nx

nx_autorun_poke.nx source

↩ module page · 62 lines · 2700 B

1// nx_autorun_poke.nx -- the EVENT trigger for nx_autorun_daemon (event-driven mode). 2// Writes ONE byte to the daemon's FIFO -> wakes it for exactly one re-prove beat. 3// Non-blocking: if no daemon is listening (no reader on the FIFO) the O_NONBLOCK 4// open returns ENXIO and the poke reports it instead of hanging. Any producer -- 5// a git post-commit hook, nx_attempt_loop after authoring, or a human -- calls this: 6// it is the ecosystem's "something changed, re-prove the rungs" signal, replacing 7// the old 6h clock. Sovereign: syscalls only, no .sh. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9const O_MAGIC_4096: i64 = 4096 10const O_MAGIC_4080: i64 = 4080 11const O_WRONLY_NB: i64 = 0x801 // O_WRONLY (1) | O_NONBLOCK (0x800) 12 13func pk_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14 15func pk_read_all(path: *u8, buf: *u8, cap: i64) -> i64 { 16 let fd: i64 = sys_openat_rd(path) 17 if fd < 0 { return 0 } 18 var n: i64 = 0 19 var go: i64 = 1 20 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap { go = 0 } } 21 sys_close(fd) 22 return n 23} 24 25func pk_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[off+i] = s[i]; i = i + 1 } return off + i } 26 27// read the event-channel FIFO path from conf (F row); default if absent. 28func pk_fifo_path(out: *u8) -> i64 { 29 let buf: *u8 = sys_mmap(O_MAGIC_4096) 30 let n: i64 = pk_read_all("knowledge/status/autorun_daemon.conf" as *u8, buf, O_MAGIC_4080) 31 var i: i64 = 0 32 while i < n { 33 var le: i64 = i 34 var scan: i64 = 1 35 while scan == 1 { if le >= n { scan = 0 } else { if buf[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } } 36 if buf[i] == (70 as u8) { 37 var o: i64 = 0 38 var q: i64 = i + 2 39 while q < le { if buf[q] == (13 as u8) { q = le } else { out[o] = buf[q]; o = o + 1; q = q + 1 } } 40 out[o] = 0 as u8 41 if o > 0 { return 1 } 42 } 43 i = le + 1 44 } 45 let d: i64 = pk_cat(out, 0, "/tmp/nx_autorun.fifo" as *u8); out[d] = 0 as u8 46 return 0 47} 48 49func main() -> i64 { 50 let fifo: *u8 = sys_mmap(256) 51 pk_fifo_path(fifo) 52 let fd: i64 = __syscall(SYS_OPENAT, AT_FDCWD, fifo as i64, O_WRONLY_NB, 0, 0, 0) 53 if fd < 0 { 54 pk_p("nx_autorun_poke: no daemon listening on " as *u8); pk_p(fifo); pk_p(" (start nx_autorun_daemon first)\n" as *u8) 55 sys_exit(1); return 1 56 } 57 sys_write(fd, "." as *u8, 1) 58 sys_close(fd) 59 pk_p("nx_autorun_poke: poked " as *u8); pk_p(fifo); pk_p(" -> one re-prove beat queued\n" as *u8) 60 sys_exit(0) 61 return 0 62}