nx_conductor.nx source
↩ module page · 119 lines · 5161 B
1// nx_conductor.nx -- the Nishi CONDUCTOR: the autonomic HEARTBEAT (sovereign).
2//
3// The role the come-alive gap implied: it makes the crew RUN itself. Each BEAT
4// it measures the car grid (hang-safe, via nx_run_timeout), prints the pulse,
5// and appends each verdict to the event-bus log (the blackboard seed) -- then
6// sleeps the cadence. This is the tick that flips the crew from "a charter a
7// human pokes" to ALIVE.
8//
9// v1 is OBSERVE-ONLY: it measures + journals, it does NOT auto-heal or
10// auto-commit. Autonomous ACTION (Doctor-heal, Builder-commit) is deliberately
11// withheld until the WARDEN bounds it (capability-trust + cardinal enforcement).
12// Live, but safe. Beats are bounded (not an infinite daemon) so it is testable;
13// v2 = unbounded daemon once the Warden is in.
14//
15// Composes runtime/nx_run_timeout.nx. Sovereign: raw syscalls, zero deps.
16// license_tier: ORIGINAL
17
18import "nx_run_timeout.nx"
19const C_MAGIC_10000: i64 = 10000
20
21const C_ACTIVE: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_compile_x86_native.elf"
22const C_EVENTLOG: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/_offc/nx_crew_events.log"
23const C_BEAT_TIMEOUT_MS: i64 = 6000 // known-good does any car <1s; >6s = poisoned
24const C_CADENCE_MS: i64 = 1000 // sleep between beats (short for the demo)
25const C_BEATS: i64 = 3 // bounded (v2 = unbounded daemon, post-Warden)
26const C_MODE_FILE: i64 = 420 // 0o644
27
28func c_wstr(fd: i64, s: *u8) -> i64 {
29 var n: i64 = 0
30 while s[n] != 0 as u8 { n = n + 1 }
31 sys_write(fd, s, n)
32 return 0
33}
34func c_wnum(fd: i64, n: i64) -> i64 {
35 if n == 0 { sys_write(fd, "0" as *u8, 1); return 0 }
36 var m: i64 = n
37 let d: *u8 = sys_mmap(24)
38 var k: i64 = 0
39 while m > 0 { d[k] = (0x30 + (m % 10)) as u8; m = m / 10; k = k + 1 }
40 var i: i64 = k - 1
41 while i >= 0 { let o: *u8 = sys_mmap(1); o[0] = d[i]; sys_write(fd, o, 1); i = i - 1 }
42 return 0
43}
44
45// measure one car: compile its canary with the active compiler, hang-safe.
46// returns nx_run_timeout's verdict (0=healthy, NX_RT_TIMEOUT=poisoned, else err).
47func c_measure(canary: *u8, elapsed_out: *i64) -> i64 {
48 let argv: *i64 = sys_mmap(32) as *i64
49 argv[0] = C_ACTIVE as i64
50 argv[1] = canary as i64
51 argv[2] = 0
52 let envp: *i64 = sys_mmap(8) as *i64
53 envp[0] = 0
54 return nx_run_timeout(C_ACTIVE, argv, envp, C_BEAT_TIMEOUT_MS, elapsed_out)
55}
56
57// append a beat event to the event-bus log (the blackboard seed; append-only).
58func c_event(beat: i64, car: *u8, verdict: *u8, ms: i64) -> i64 {
59 let fd: i64 = sys_openat_append(C_EVENTLOG, C_MODE_FILE)
60 if fd < 0 { return 0 - 1 }
61 c_wstr(fd, "beat="); c_wnum(fd, beat)
62 c_wstr(fd, " car="); c_wstr(fd, car)
63 c_wstr(fd, " verdict="); c_wstr(fd, verdict)
64 c_wstr(fd, " ms="); c_wnum(fd, ms)
65 c_wstr(fd, "\n")
66 sys_close(fd)
67 return 0
68}
69
70func main() -> i64 {
71 c_wstr(1, "NISHI CONDUCTOR -- the autonomic heartbeat (OBSERVE-ONLY; action gated by the Warden)\n")
72 c_wstr(1, "====================================================================================\n")
73
74 // car registry (name + canary .nx). Compiler-health is the upstream keystone.
75 let N: i64 = 2
76 let names: *i64 = sys_mmap(N * 8) as *i64
77 let cans: *i64 = sys_mmap(N * 8) as *i64
78 names[0] = ("crypto-sha256 " as *u8) as i64
79 cans[0] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_sha256_kat_test.nx" as *u8) as i64
80 names[1] = ("compiler-health " as *u8) as i64
81 cans[1] = ("/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_slice_contour.nx" as *u8) as i64
82
83 var beat: i64 = 0
84 var poisoned_seen: i64 = 0
85 while beat < C_BEATS {
86 let bt0: i64 = sys_now_ms()
87 c_wstr(1, "beat "); c_wnum(1, beat); c_wstr(1, ":\n")
88 var i: i64 = 0
89 while i < N {
90 let elb: *i64 = sys_mmap(8) as *i64
91 elb[0] = 0
92 let rc: i64 = c_measure(cans[i] as *u8, elb)
93 var verdict: *u8 = "OK " as *u8
94 if rc != 0 {
95 if rc == NX_RT_TIMEOUT { verdict = "POISON " as *u8; poisoned_seen = poisoned_seen + 1 }
96 else { verdict = "ERR " as *u8 }
97 }
98 c_wstr(1, " "); c_wstr(1, names[i] as *u8); c_wstr(1, " "); c_wstr(1, verdict)
99 c_wstr(1, " "); c_wnum(1, elb[0]); c_wstr(1, "ms\n")
100 c_event(beat, names[i] as *u8, verdict, elb[0])
101 i = i + 1
102 }
103 let bms: i64 = sys_now_ms() - bt0
104 c_wstr(1, " beat-clock "); c_wnum(1, bms); c_wstr(1, "ms")
105 if bms > C_MAGIC_10000 { c_wstr(1, " [SLOW >10s -- speed the feedback]") }
106 c_wstr(1, "\n")
107 sys_sleep_ms(C_CADENCE_MS)
108 beat = beat + 1
109 }
110
111 c_wstr(1, "HEARTBEAT: "); c_wnum(1, C_BEATS); c_wstr(1, " beats measured + journaled to the event-bus.\n")
112 if poisoned_seen > 0 {
113 c_wstr(1, " compiler-health POISONED -> needs Doctor-heal. OBSERVE-ONLY: the heal is an\n")
114 c_wstr(1, " autonomous ACTION, deliberately gated by the WARDEN (the next build). Live, safe.\n")
115 } else {
116 c_wstr(1, " all cars healthy this run.\n")
117 }
118 return 0
119}