code wiki / _hdl_build / nx_arbiter_serve.nx

nx_arbiter_serve.nx source

↩ module page · 32 lines · 2352 B

1// nx_arbiter_serve.nx -- SERVE-WRAPPER making the ARBITER role's capability an MCP-callable self-test (operator 2026-07-10: 2// "each role owns its raci capabilities and gets them to be mcp apis"). The library-role pattern: nx_arbiter is a LIBRARY 3// (fl_try/fl_acquire/fl_release, no main -> not fork-exec'able); this tiny wrapper IMPORTS it and EXERCISES the capability 4// (acquire a lock, prove a contending try is BLOCKED = mutual exclusion, release, prove re-acquire succeeds) -> clean exit 0 + 5// JSON verdict, so nishi_crew can dispatch `arbiter arbitrate`. Self-contained (flock on ~/.nishi/locks, no external data). 6// Import ONLY nx_arbiter.nx (it brings nx_syscalls transitively; a second direct import = the double-import rc=6 trap). 7// JSON trick: asv_w2 converts ' (39) -> " (34). license_tier: ORIGINAL expect_exit: 0 8import "nx_arbiter.nx" 9 10func asv_raw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func asv_w2(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){ if s[n]==(39 as u8) { let q: *u8=sys_mmap(1); q[0]=34 as u8; sys_write(1,q,1) } else { sys_write(1,(s as i64+n) as *u8,1) } n=n+1 } return 0 } 12func asv_bit(b: i64) -> i64 { let d: *u8=sys_mmap(1); d[0]=(48+b) as u8; sys_write(1,d,1); return 0 } 13 14func main() -> i64 { 15 let res: *u8 = "nishi_crew_arbiter_selftest" as *u8 16 let fd1: i64 = fl_try(res) 17 var acquired: i64 = 0; if fd1 >= 0 { acquired = 1 } 18 let fd2: i64 = fl_try(res) 19 var blocked: i64 = 0; if fd2 < 0 { blocked = 1 } else { fl_release(fd2) } 20 if fd1 >= 0 { fl_release(fd1) } 21 let fd3: i64 = fl_try(res) 22 var reacq: i64 = 0; if fd3 >= 0 { reacq = 1; fl_release(fd3) } 23 var ok: i64 = 0; if acquired==1 { if blocked==1 { if reacq==1 { ok=1 } } } 24 asv_w2("{'v':1,'api':'nishi-crew-arbiter/v1','role':'arbiter','activity':'arbitrate','capability':'conflict-free advisory locking (flock mutual-exclusion for parallel workstreams)','selftest':{'acquired':" as *u8); asv_bit(acquired) 25 asv_w2(",'contended_try_blocked':" as *u8); asv_bit(blocked) 26 asv_w2(",'reacquired_after_release':" as *u8); asv_bit(reacq) 27 asv_w2("},'verdict':'" as *u8) 28 if ok==1 { asv_w2("GREEN (mutual exclusion proven)'}" as *u8) } else { asv_w2("RED'}" as *u8) } 29 asv_w2("\n" as *u8) 30 if ok==1 { sys_exit(0); return 0 } 31 sys_exit(1); return 1 32}