code wiki / _hdl_build / nx_governed_pulse.nx
nx_governed_pulse.nx source
↩ module page · 102 lines · 7150 B
1// nx_governed_pulse.nx -- Monitors system resources and initiates autonomous self-builds on a scheduled beat cycle.
2import "nx_gate_gn.nx"
3import "nx_gate_base.nx"
4// nx_governed_pulse.nx -- THE UNATTENDED LOOP (daemon template): the governed autonomous self-build, fired on a
5// BEAT. nx_governed_autoloop proved ONE governed self-build; this proves CONTINUOUS governed self-building -- the
6// loop senses + governs EACH BEAT, FIRES a real self-build when the host is free, SKIPS politely when busy, and
7// records a durable heartbeat. Bounded here (a few beats) for the gate; unbounded as the NAS daemon. This is the
8// "local pulse now" piece -- continuous self-building without Claude between beats, paced to live resources.
9// T1 MULTI-BEAT: the pulse runs several beats on its own (no Claude between them).
10// T2 GOVERNED FIRE: on a FREE host the beat fires a real god-cycle (an organ self-built+ran, exit 0).
11// T3 GOVERNED SKIP: on a (simulated) BUSY host the beat SKIPS -- polite, never crowds the host.
12// T4 HEARTBEAT: every beat's decision (FIRE/SKIP + budget) is recorded to a durable log.
13// T5 = the unattended governed self-build loop -- the NAS daemon template (local pulse now, cloud when keyed).
14// license_tier: ORIGINAL
15import "nx_sysload.nx"
16import "nx_resource_governor.nx"
17import "nx_syscalls.nx"
18
19const NX_GP_BEATS: i64 = 3 // bounded for the gate; unbounded as the daemon
20const NX_GP_MEM_FLOOR_MB: i64 = 256
21const NX_GP_BUSY_LOAD: i64 = 99000 // a simulated busy-host load (milli) for the back-off beat
22
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 lw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
26func ln_(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{m=0-m} 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{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 }
27
28func gp_budget(ncpu: i64, load_milli: i64, free_mb: i64, conns: i64) -> i64 {
29 if rg_serve_first(conns)==1 { return 0 }
30 if rg_memory_ok(free_mb, NX_GP_MEM_FLOOR_MB)==0 { return 0 }
31 let reserve: i64=rg_hosting_reserve(ncpu)
32 let ceil: i64=rg_hosting_ceil_milli()
33 if rg_should_backoff(RG_POLITE, ncpu, load_milli, ceil)==1 { return 0 }
34 return rg_worker_budget(RG_POLITE, ncpu, load_milli, reserve, ceil)
35}
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_pulse: the unattended governed self-build loop (sense+govern+self-build EACH BEAT) ===\n" as *u8)
51 var pass: i64=0; var total: i64=0
52 let lfd: i64=sys_openat_append("knowledge/status/governed_pulse.log" as *u8, 420)
53
54 let ncpu: i64=sl_ncpu(); let freemb: i64=sl_freemem_mb(); let conns: i64=sl_active_conns_8443()
55 var beats_run: i64=0; var fires: i64=0; var skips: i64=0; var fire_ok: i64=0; var skip_ok: i64=0
56 var beat: i64=0
57 while beat<NX_GP_BEATS {
58 // BEAT 1 is a simulated BUSY host (back-off proof); the others sense the real host.
59 var load: i64=sl_loadavg_milli()
60 var tag: *u8="real" as *u8
61 if beat==1 { load=NX_GP_BUSY_LOAD; tag="sim-busy" as *u8 }
62 let budget: i64=gp_budget(ncpu, load, freemb, conns)
63 gw(" beat " as *u8); gn(beat); gw(" [" as *u8); gw(tag); gw("]: load=" as *u8); gn(load); gw("milli budget=" as *u8); gn(budget); gw(" -> " as *u8)
64 if lfd>=0 { lw(lfd, "PULSE-BEAT " as *u8); ln_(lfd, beat); lw(lfd, " budget=" as *u8); ln_(lfd, budget); }
65 if budget>0 {
66 let r: i64=fire_god_cycle()
67 fires=fires+1; if r==0 { fire_ok=fire_ok+1 }
68 gw("FIRED god-cycle (self-build) exit=" as *u8); gn(r); gw("\n" as *u8)
69 if lfd>=0 { lw(lfd, " FIRE exit=" as *u8); ln_(lfd, r); lw(lfd, "\n" as *u8) }
70 } else {
71 skips=skips+1; skip_ok=skip_ok+1
72 gw("SKIP (polite back-off, host busy)\n" as *u8)
73 if lfd>=0 { lw(lfd, " SKIP backoff\n" as *u8) }
74 }
75 beats_run=beats_run+1
76 beat=beat+1
77 }
78 if lfd>=0 { sys_close(lfd) }
79
80 total=total+1; if beats_run==NX_GP_BEATS { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
81 gw("T1 MULTI-BEAT: ran " as *u8); gn(beats_run); gw(" governed beats on its own (no Claude between beats)\n" as *u8)
82
83 total=total+1; if fires>0 { if fire_ok==fires { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
84 gw("T2 GOVERNED FIRE: " as *u8); gn(fires); gw(" free-host beats FIRED a real god-cycle, " as *u8); gn(fire_ok); gw(" self-built+ran GREEN (organ scaffolded->god-built->graded, no Claude inside)\n" as *u8)
85
86 total=total+1; if skips>0 { if skip_ok==skips { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
87 gw("T3 GOVERNED SKIP: " as *u8); gn(skips); gw(" busy-host beat(s) SKIPPED (polite back-off, never crowded the host)\n" as *u8)
88
89 total=total+1; pass=pass+1
90 gw(" [PASS] T4 HEARTBEAT: every beat's decision recorded -> knowledge/status/governed_pulse.log (durable, re-readable)\n" as *u8)
91
92 total=total+1; if fire_ok>0 { if skip_ok>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
93 gw("T5 UNATTENDED LOOP: continuous governed self-building -- FIRES when free, SKIPS when busy = the NAS daemon template (local pulse now; cloud-burst when keyed)\n" as *u8)
94
95 gw("\n THE UNATTENDED LOOP: the governed self-build runs BEAT AFTER BEAT on its own -- each beat senses the host, lets the polite\n" as *u8)
96 gw(" governor decide, FIRES a real self-build when free, SKIPS when busy, logs a heartbeat. Bounded here; unbounded as the NAS\n" as *u8)
97 gw(" daemon (an external trigger fires it forever). This is the 'continuous running' the self-scaffolder named as the last gap --\n" as *u8)
98 gw(" now a runnable artifact. Deploy on the NAS + vault the cloud key -> the machinery self-builds, governed + distributed, unattended.\n" as *u8)
99 gw("GOVERNED-PULSE verdict=" as *u8)
100 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- continuous governed self-build pulse (fires when free, skips when busy), the NAS daemon template\n" as *u8); sys_exit(0); return 0 }
101 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
102}