code wiki / _hdl_build / nx_runpath.nx
nx_runpath.nx source
↩ module page · 78 lines · 4146 B
1// nx_runpath.nx -- ISOLATION primitive for conflict-free PARALLEL workstreams (operator 2026-06-20:
2// ~21 parallel Claude workstreams shared ONE /tmp and stepped on each other -- F-class coordination).
3// Instead of hardcoded /tmp/<file> (shared, raced, shredded by siblings, AND churned by systemd-tmpfiles),
4// every transient file goes in a PER-WORKSTREAM namespace under a STABLE dir: <RUNDIR>/<wsid>/<name>.
5// RUNDIR = /home/elderwesto/.nishi/run (ext4, NOT /tmp -> not systemd-tmpfiles-managed -> stable)
6// wsid = the workstream id (a process/session id); siblings get different wsids -> CANNOT collide.
7// This is the hardware-rung-up fix: isolation BY CONSTRUCTION (OS dir namespace), not retry-through-hope.
8//
9// WIRING PLAN (the coordinated install -- do NOT hot-swap while siblings are live): replace the hardcoded
10// /tmp/nxpass + /tmp/nxsecret.out + /tmp/<organ>.sov.elf in nx_machine_key / nx_vault / nx_aw_push /
11// nx_sov_build_run (the chain passes ONE wsid down so the forked machine-key+vault agree) with rp_path(wsid,..).
12// No main -- library. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14const RP_MAGIC_65536: i64 = 65536
15const RP_MAGIC_65535: i64 = 65535
16
17const RP_RUNDIR: *u8 = "/home/elderwesto/.nishi/run"
18
19func rp_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
20func rp_itoa(v: i64, dst: *u8) -> i64 { var m: i64=v; 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; while i<k{dst[i]=t[k-1-i];i=i+1} dst[k]=0 as u8; return k }
21
22// does buf[at..] begin with lit (NUL-terminated)?
23func rp_match(buf: *u8, at: i64, n: i64, lit: *u8) -> i64 {
24 var i: i64 = 0
25 while lit[i] != (0 as u8) { if at + i >= n { return 0 } if buf[at+i] != lit[i] { return 0 } i = i + 1 }
26 return 1
27}
28// per-workstream id = the NISHI_WSID environment variable (inherited by ALL the session's processes, so the
29// build -> push -> forked machine-key/vault all agree on ONE id). Read sovereignly from /proc/self/environ
30// (NUL-separated KEY=VALUE). Falls back to "shared" if unset. fills wsid_out; returns length.
31func rp_wsid(wsid_out: *u8) -> i64 {
32 let fd: i64 = sys_openat_rd("/proc/self/environ" as *u8)
33 if fd >= 0 {
34 let buf: *u8 = sys_mmap(RP_MAGIC_65536)
35 let n: i64 = sys_read(fd, buf, RP_MAGIC_65535)
36 sys_close(fd)
37 var i: i64 = 0; var entry: i64 = 1
38 while i < n {
39 if entry == 1 { if rp_match(buf, i, n, "NISHI_WSID=" as *u8) == 1 {
40 var j: i64 = i + 11; var k: i64 = 0
41 while j < n { if buf[j] == (0 as u8) { j = n } else { wsid_out[k] = buf[j]; k = k + 1; j = j + 1 } }
42 wsid_out[k] = 0 as u8
43 if k > 0 { return k }
44 } }
45 if buf[i] == (0 as u8) { entry = 1 } else { entry = 0 }
46 i = i + 1
47 }
48 }
49 let s: *u8 = "shared" as *u8; var z: i64 = 0
50 while s[z] != (0 as u8) { wsid_out[z] = s[z]; z = z + 1 } wsid_out[z] = 0 as u8
51 return z
52}
53
54// out = "<RUNDIR>/<wsid>/<name>" (NUL-terminated); returns length. (RUNDIR bound to a local before
55// indexing -- indexing a top-level const *u8 directly desyncs the nx parser.)
56func rp_path(wsid: *u8, name: *u8, out: *u8) -> i64 {
57 let rd: *u8 = RP_RUNDIR
58 var o: i64 = 0; var i: i64 = 0
59 while rd[i]!=(0 as u8){ out[o]=rd[i]; o=o+1; i=i+1 }
60 out[o]=47 as u8; o=o+1
61 i=0; while wsid[i]!=(0 as u8){ out[o]=wsid[i]; o=o+1; i=i+1 }
62 out[o]=47 as u8; o=o+1
63 i=0; while name[i]!=(0 as u8){ out[o]=name[i]; o=o+1; i=i+1 }
64 out[o]=0 as u8
65 return o
66}
67
68// ensure <RUNDIR> and <RUNDIR>/<wsid> exist (mkdir each, 0700; EEXIST harmless). returns 0.
69func rp_ensure(wsid: *u8) -> i64 {
70 let rd: *u8 = RP_RUNDIR
71 __syscall(83, rd as i64, 448, 0, 0, 0, 0) // mkdir RUNDIR 0700
72 let d: *u8 = sys_mmap(512); var o: i64=0; var i: i64=0
73 while rd[i]!=(0 as u8){ d[o]=rd[i]; o=o+1; i=i+1 }
74 d[o]=47 as u8; o=o+1
75 i=0; while wsid[i]!=(0 as u8){ d[o]=wsid[i]; o=o+1; i=i+1 } d[o]=0 as u8
76 __syscall(83, d as i64, 448, 0, 0, 0, 0) // mkdir RUNDIR/wsid 0700
77 return 0
78}