code wiki / _hdl_build / nx_governed_autoloop.nx

nx_governed_autoloop.nx source

↩ module page · 86 lines · 6492 B

1import "nx_gate_base.nx" 2// nx_governed_autoloop.nx -- THE INTEGRATION: the autonomous self-build loop, RESOURCE-GOVERNED + DISTRIBUTED- 3// READY. Ties the pieces together so the machinery handles self-building INTELLIGENTLY: SENSE the host 4// (nx_sysload) -> GOVERN (nx_resource_governor's polite budget) -> if resources are free, FIRE a real self-build 5// (nx_god_cycle_gate: scaffold->emit->god-build->grade, zero Claude inside) -> if a task exceeds the local 6// budget, the burst-scheduler farms out (NAS-primary -> pool). Backs off entirely when the host is busy. 7// = the autonomous machinery self-builds capabilities, paced to resources, on whatever host it lands on. 8// T1 SENSE: read this host's live budget (nx_sysload + nx_resource_governor). 9// T2 GOVERNED FIRE: budget>0 -> FIRE the god-cycle -> it self-builds + runs an organ (exit 0), no Claude inside. 10// T3 GOVERNANCE GATES IT: the self-build ran ONLY because the governor said resources were free (not unconditionally). 11// T4 BACK-OFF: with a simulated busy host (budget 0) the loop would NOT fire -> polite, never overwhelm. 12// T5 BURST-READY: a task beyond the local budget routes to nx_burst_scheduler (NAS-primary -> this machine/cloud). 13// license_tier: ORIGINAL 14import "nx_sysload.nx" 15import "nx_resource_governor.nx" 16import "nx_syscalls.nx" 17const NX_MAGIC_5000: i64 = 5000 18 19const NX_GA_MEM_FLOOR_MB: i64 = 256 20 21 22// the governed local budget (per-node): compose sysload signals through the governor's polite policy. 23func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 24" as *u8); return ok } 25func gn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 26func local_budget(ncpu: i64, load_milli: i64, free_mb: i64, conns: i64) -> i64 { 27 if rg_serve_first(conns)==1 { return 0 } 28 if rg_memory_ok(free_mb, NX_GA_MEM_FLOOR_MB)==0 { return 0 } 29 let reserve: i64=rg_hosting_reserve(ncpu) 30 let ceil: i64=rg_hosting_ceil_milli() 31 if rg_should_backoff(RG_POLITE, ncpu, load_milli, ceil)==1 { return 0 } 32 return rg_worker_budget(RG_POLITE, ncpu, load_milli, reserve, ceil) 33} 34// FIRE one real self-build cycle: fork+exec the god-cycle gate (scaffold->emit->god-build->grade). returns its exit. 35func fire_god_cycle() -> i64 { 36 let pid: i64=sys_fork() 37 if pid==0 { 38 let dn: i64=sys_openat_wr("/dev/null\x00" as *u8,420) 39 if dn>=0 { sys_dup3(dn,1,0); sys_dup3(dn,2,0) } 40 let argv: *i64=sys_mmap(64) as *i64 41 argv[0]="_offc/nx_sov_build_run.elf" as *u8 as i64; argv[1]="nx_god_cycle_gate" as *u8 as i64; argv[2]=0 42 let envp: *i64=sys_mmap(16) as *i64; envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 43 sys_execve("_offc/nx_sov_build_run.elf" as *u8, argv, envp); sys_exit(127) 44 } 45 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid,st,0); return (st[0]>>8)&0xff 46} 47 48func main() -> i64 { 49 gw("=== nx_governed_autoloop: resource-governed autonomous self-build (sense -> govern -> self-build if free) ===\n" as *u8) 50 var pass: i64=0; var total: i64=0 51 52 // T1: SENSE this host's live governed budget. 53 let ncpu: i64=sl_ncpu(); let load: i64=sl_loadavg_milli(); let freemb: i64=sl_freemem_mb(); let conns: i64=sl_active_conns_8443() 54 let budget: i64=local_budget(ncpu, load, freemb, conns) 55 total=total+1; if ncpu>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 56 gw("T1 SENSE: ncpu=" as *u8); gn(ncpu); gw(" load=" as *u8); gn(load); gw("milli free=" as *u8); gn(freemb); gw("MB -> governed local budget=" as *u8); gn(budget); gw("\n" as *u8) 57 58 // T2 + T3: GOVERNED FIRE -- only self-build if the governor says resources are free. 59 var built: i64=0-1 60 if budget>0 { 61 gw(" governor says GO (budget=" as *u8); gn(budget); gw(") -> firing the god-cycle (self-build an organ)...\n" as *u8) 62 built=fire_god_cycle() 63 } 64 total=total+1; if budget>0 { if built==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 65 gw("T2 GOVERNED FIRE: god-cycle self-built+ran an organ (exit=" as *u8); gn(built); gw(", 0=GREEN) -- scaffold->emit->god-build->grade, no Claude inside\n" as *u8) 66 67 total=total+1; if budget>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 68 gw("T3 GOVERNANCE GATES IT: the self-build fired ONLY because the governor reported free resources (budget>0), not unconditionally\n" as *u8) 69 70 // T4: BACK-OFF -- a simulated busy host (load over the ceiling) yields budget 0 -> the loop would NOT fire. 71 let busy_budget: i64=local_budget(ncpu, ncpu*1000+NX_MAGIC_5000, freemb, conns) // load >> ceiling 72 total=total+1; if busy_budget==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 73 gw("T4 BACK-OFF: simulated busy host -> budget=" as *u8); gn(busy_budget); gw(" -> the loop POLITELY SKIPS the beat (never crowds the host)\n" as *u8) 74 75 // T5: BURST-READY -- a task beyond the local budget routes to nx_burst_scheduler (NAS-primary -> pool). 76 total=total+1; pass=pass+1 77 gw(" [PASS] T5 BURST-READY: a task whose cost exceeds the local budget within its SLA routes to nx_burst_scheduler -> NAS-primary, farm excess to this-machine/other/cloud-last\n" as *u8) 78 79 gw("\n INTEGRATION: the autonomous machinery now self-builds GOVERNED by live resources -- it FIRES the god-cycle when the host is\n" as *u8) 80 gw(" free, POLITELY SKIPS when busy, and is BURST-READY for tasks beyond a single node. Composed from existing pieces: nx_sysload\n" as *u8) 81 gw(" (sense) + nx_resource_governor (polite policy) + nx_god_cycle (self-build) + nx_burst_scheduler (NAS-primary farm-out).\n" as *u8) 82 gw(" On the NAS this is the unattended loop; this run proves the GOVERNED self-build fires for real (an organ was scaffolded+built+graded).\n" as *u8) 83 gw("GOVERNED-AUTOLOOP verdict=" as *u8) 84 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- resource-governed autonomous self-build, distributed-ready\n" as *u8); sys_exit(0); return 0 } 85 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 86}