nx_segsweep.nx
buildroot/runtime/nx_segsweep.nx
about
nx_segsweep.nx -- SOVEREIGN seg-store sweeper. The organ that retires nx_segguard.sh (2026-08-06).
OPERATOR DOCTRINE: ".sh are not nishi ecosystem". nx_segguard.sh was a SHELL WRAPPER around an organ
that already existed -- it parsed segguard.conf with sed+head, enumerated the planes, and forked
nx_seg_compact_cli.elf ONCE PER PLANE under a lock. The real work is ss_compact_cap in nx_seg_store.nx,
a LIBRARY function callable in-process. With ~1087 manifests under knowledge/store that wrapper paid a
SUBSHELL PLUS A CLI FORK PER PLANE, plus date/sed/head/mkdir every tick.
MEASURED COST OF THE WRAPPER: 717s per run against a DECLARED 600s period (three independent
measurements: 655s, 700s+, 717s), and a sibling seat measured it consuming 1399s of an 1800s clock
window = 78pct, starving 31 of 45 jobs. It is also the direct source of the RED forks_per_sec=57 in
nx_procchurn. This organ replaces ~2x1087 forks with ZERO.
(STAR)A SHELL SCRIPT THAT WRAPS A SOVEREIGN ORGAN PAYS A PROCESS PER ITEM TO DO WORK THE LIBRARY WOULD
DO PER CALL -- THE COST IS NOT THE ALGORITHM, IT IS THE WRAPPER.
(STAR)THE SLOWEST THING ON A SOVEREIGN BOX IS THE PART THAT IS NOT SOVEREIGN YET.
THE ONE THING THIS MUST NOT INHERIT: nx_seg_compact_cli.elf takes a BLOCKING flock(LOCK_EX) and holds
it until PROCESS EXIT. Its own header records what that cost: "48 nx_segguard.sh + 24 nx_seg_compact_cli
stacked, all State=S, oldest ~6h, thread count climbing ~260/hr on a box already 92pct into swap."
A blocking lock is survivable in a fork-per-plane design because the process dies; in a SINGLE
long-lived sweeper it would wedge the whole sweep forever. So this organ takes LOCK_EX|LOCK_NB and
SKIPS a contended plane, and CLOSES the fd every iteration so locks can never accumulate.
(STAR)A LOCK DISCIPLINE THAT IS SAFE ONLY BECAUSE THE PROCESS EXITS IS NOT SAFE IN A PROCESS THAT DOES NOT.
BOUNDED + RESUMABLE (rule 10, idempotent): a wall-clock budget caps one run, and a persisted CURSOR
makes the next run continue where this one stopped. A budget WITHOUT a cursor would re-scan the same
alphabetical head forever and never reach the tail -- silent permanent blindness for whatever sorts last.
nx_segsweep [threshold] [budget_secs]
license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_seg_store.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 32 | const SW_STORE: *u8 = "knowledge/store" |
| 33 | const SW_CONF: *u8 = "knowledge/status/segguard.conf" |
| 34 | const SW_LOG: *u8 = "knowledge/status/segsweep.log" |
| 35 | const SW_CURSOR: *u8 = "knowledge/status/segsweep.cursor" |
| 36 | const SW_SUFFIX: *u8 = "-manifest.txt" |
| 37 | const SW_DIRBUF: i64 = 262144 |
| 38 | const SW_NAMEW: i64 = 256 |
| 39 | const SW_MAXPLANES: i64 = 4096 |
| 40 | const SW_LOCK_NB: i64 = 6 // LOCK_EX(2) | LOCK_NB(4) -- NEVER plain LOCK_EX here, see header |
| 41 | const SW_MODE: i64 = 0x1a4 |
| 42 | const SW_DEF_THRESH: i64 = 8 |
| 43 | const SW_DEF_BUDGET: i64 = 120 |
| 44 | const SW_COMPACT_CAP: i64 = 100000 |
| 47 | const SW_CLI: *u8 = "/volume1/homes/elderwesto/nishihost/nx_seg_compact_cli.elf" |
| 50 | const SW_CLI_TIMEOUT: i64 = 300 |
| 59 | const SW_MAXFOLD: i64 = 256 |
| 65 | const SW_REAP_TRIES: i64 = 20 |
| 77 | const SW_POISON: *u8 = "knowledge/status/segsweep_poison.txt" |
| 78 | const SW_POISONCAP: i64 = 65536 |
functions
| 80 | func sw_o(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 81 | func sw_cat(d: *u8, o: i64, s: *u8) -> i64 { var x: i64=o; var i: i64=0; while s[i]!=(0 as u8){d[x]=s[i];x=x+1;i=i+1} return x } |
| 82 | func sw_num(d: *u8, o: i64, v: i64) -> i64 { var x: i64=o; var mm: i64=v; if mm<0{d[x]=45 as u8;x=x+1;mm=0-mm} if mm==0{d[x]=48 as u8;return x+1} let t:*u8=sys_mmap(24); var k:i64=0; while mm>0{t[k]=(48+(mm%10)) as u8;mm=mm/10;k=k+1} var j:i64=0; while j<k{d[x]=t[k-1-j];x=x+1;j=j+1} return x } |
| 83 | func sw_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } |
| 87 | func sw_opendir(p: *u8) -> i64 { return __syscall(257, 0-100, p as i64, 0x10000, 0, 0, 0) } called by 1: main |
| 88 | func sw_slot(base: *u8, i: i64) -> *u8 { return ((base as i64) + i*SW_NAMEW) as *u8 } called by 1: main |
| 97 | func sw_poisoned(pois: *u8, pn: i64, pfx: *u8) -> i64 |
| 113 | func sw_poison_add(pfx: *u8) -> i64 |
| 121 | func sw_beat(tag: *u8, pfx: *u8, idx: i64, live: i64) -> i64 |
| 133 | func sw_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ if s[i]<(48 as u8){return v} if s[i]>(57 as u8){return v} v=v*10+((s[i]-(48 as u8)) as i64); i=i+1 } return v } called by 1: main |
| 136 | func sw_is_manifest(nm: *u8) -> i64 |
| 146 | func sw_conf_int(key: *u8, dflt: i64) -> i64 |
| 188 | func main(argc: i64, argv: *i64) -> i64 |