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