code wiki / (root) / nx_swarm_admit.nx

nx_swarm_admit.nx source

↩ module page · 101 lines · 3964 B

1// nx_swarm_admit.nx -- THE GENERAL ECOSYSTEM ADMISSION CAPABILITY (operator 2026-07-16: host- 2// oversubscription control is the SWARM's job as the cluster scheduler, not a forge hack). Any 3// workstream's heavy launcher (forge model decode, codec, image-gen, training, a big build) calls 4// admit BEFORE starting; the swarm returns GRANT/QUEUE/DENY from LIVE host telemetry so the box 5// never hits the load-105 collision (two uncoordinated model workloads). Supersedes the forge- 6// specific nx_model_lane by generalizing it: workstream+kind keyed, telemetry-backed, one verdict. 7// sa_verdict(load1, ncpu, avail_mb, est_mb, floor_mb, held, max_conc) -- PURE, gate-testable: 8// DENY_LOAD if load1>=ncpu · DENY_RAM if avail<est+floor · QUEUE if held>=max_conc · else GRANT. 9// Composes nx_model_lane_core (proven /proc telemetry: ml_load1/ml_ncores/ml_meminfo_avail_mb) -- 10// DRY, no reinvention. RUNG 2 = fairness via nx_resource_arbiter (nx_ra_request) + auto cross-proc 11// holder ledger; RUNG 3 = beacon nb_loadavg_milli canonical source + MCP-expose on the swarm plane. 12// admit <workstream> <kind> <est_mb> <held> <max_conc> exit = verdict (0 GRANT/1 QUEUE/2/3 DENY) 13// license_tier: ORIGINAL 14import "nx_model_lane_core.nx" 15 16const SA_GRANT: i64 = 0 17const SA_QUEUE: i64 = 1 18const SA_DENY_LOAD: i64 = 2 19const SA_DENY_RAM: i64 = 3 20const SA_FLOOR_MB: i64 = 2048 // headroom floor above est (data-driven default; overridable later via svc-config) 21 22// PURE admission policy (deterministic; the gate exercises the whole table without forcing real load). 23func sa_verdict(load1: i64, ncpu: i64, avail_mb: i64, est_mb: i64, floor_mb: i64, held: i64, max_conc: i64) -> i64 { 24 if ncpu > 0 { 25 if load1 >= ncpu { return SA_DENY_LOAD } 26 } 27 if avail_mb >= 0 { 28 if avail_mb < est_mb + floor_mb { return SA_DENY_RAM } 29 } 30 if held >= max_conc { return SA_QUEUE } 31 return SA_GRANT 32} 33 34func sa_verdict_name(v: i64) -> *u8 { 35 if v == SA_GRANT { return "GRANT" as *u8 } 36 if v == SA_QUEUE { return "QUEUE" as *u8 } 37 if v == SA_DENY_LOAD { return "DENY-LOAD" as *u8 } 38 if v == SA_DENY_RAM { return "DENY-RAM" as *u8 } 39 return "DENY" as *u8 40} 41 42// live admit: read host telemetry (via model_lane_core /proc helpers), apply the pure verdict. 43func sa_admit_live(workstream: *u8, kind: *u8, est_mb: i64, held: i64, max_conc: i64) -> i64 { 44 let l1: i64 = ml_load1() 45 let nc: i64 = ml_ncores() 46 let avail: i64 = ml_meminfo_avail_mb() 47 let v: i64 = sa_verdict(l1, nc, avail, est_mb, SA_FLOOR_MB, held, max_conc) 48 std_puts("SWARM-ADMIT ws=" as *u8) 49 std_puts(workstream) 50 std_puts(" kind=" as *u8) 51 std_puts(kind) 52 std_puts(" load1=" as *u8) 53 std_pdec(l1) 54 std_puts("/ncpu=" as *u8) 55 std_pdec(nc) 56 std_puts(" avail_mb=" as *u8) 57 std_pdec(avail) 58 std_puts(" est_mb=" as *u8) 59 std_pdec(est_mb) 60 std_puts(" held=" as *u8) 61 std_pdec(held) 62 std_puts("/" as *u8) 63 std_pdec(max_conc) 64 std_puts(" -> " as *u8) 65 std_putln(sa_verdict_name(v)) 66 return v 67} 68 69func main(argc: i64, argv: *i64) -> i64 { 70 if argc < 6 { 71 std_putln("usage: nx_swarm_admit admit <workstream> <kind> <est_mb> <held> <max_conc>" as *u8) 72 sys_exit(4) 73 return 4 74 } 75 let a1: i64 = argv[1] 76 let verb: *u8 = a1 as *u8 77 if std_streq(verb, "admit" as *u8) == 0 { 78 std_putln("unknown verb (only: admit)" as *u8) 79 sys_exit(4) 80 return 4 81 } 82 let a2: i64 = argv[2] 83 let ws: *u8 = a2 as *u8 84 let a3: i64 = argv[3] 85 let kind: *u8 = a3 as *u8 86 let a4: i64 = argv[4] 87 let p4: *u8 = a4 as *u8 88 let est: i64 = std_atoi(p4) 89 let a5: i64 = argv[5] 90 let p5: *u8 = a5 as *u8 91 let held: i64 = std_atoi(p5) 92 var maxc: i64 = 1 93 if argc >= 7 { 94 let a6: i64 = argv[6] 95 let p6: *u8 = a6 as *u8 96 maxc = std_atoi(p6) 97 } 98 let v: i64 = sa_admit_live(ws, kind, est, held, maxc) 99 sys_exit(v) 100 return v 101}