code wiki / _hdl_build / nx_ecomat_beat.nx
nx_ecomat_beat.nx source
↩ module page · 131 lines · 7991 B
1// nx_ecomat_beat.nx -- R3 THE FLYWHEEL BEAT: one command runs the whole loop once --
2// MEASURE (from the sovereign store + live-derived grades) -> TARGET (feed ranked work to the loop)
3// -> PUBLISH (re-emit the dashboard) -> log a beat record. This is the body the conductor calls every
4// beat so the ecosystem grows itself untouched. argv overrides every path (the gate points it at /tmp).
5// license_tier: ORIGINAL
6import "nx_ecomat_lib.nx"
7import "nx_registry_lock.nx"
8import "nx_ecomat_domledger_lib.nx"
9
10// run a sovereign organ by name (/tmp/<name>.sov.elf, heal-rebuild via nx_sov_build_run if missing) -- the proven
11// tp_run pattern. stdout/stderr -> /dev/null so the beat stays clean; the organ's value is its written log/output.
12func eb_run(name: *u8) -> i64 {
13 let path: *u8 = sys_mmap(256)
14 var o: i64 = 0
15 let p1: *u8 = "/tmp/" as *u8
16 var i: i64 = 0
17 while p1[i] != (0 as u8) { path[o] = p1[i]; o = o + 1; i = i + 1 }
18 i = 0
19 while name[i] != (0 as u8) { path[o] = name[i]; o = o + 1; i = i + 1 }
20 let p2: *u8 = ".sov.elf" as *u8
21 i = 0
22 while p2[i] != (0 as u8) { path[o] = p2[i]; o = o + 1; i = i + 1 }
23 path[o] = 0 as u8
24 var heal: i64 = 0
25 let probe: i64 = sys_openat_rd(path)
26 if probe >= 0 { sys_close(probe) } else { heal = 1 }
27 let pid: i64 = sys_fork()
28 if pid == 0 {
29 let argv2: *i64 = sys_mmap(32) as *i64
30 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = 0
31 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4); if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
32 if heal == 1 {
33 let sbr: *u8 = "_offc/nx_sov_build_run.elf" as *u8
34 argv2[0] = sbr as i64; argv2[1] = name as i64; argv2[2] = 0
35 sys_execve(sbr, argv2, envp)
36 }
37 argv2[0] = path as i64; argv2[1] = 0
38 sys_execve(path, argv2, envp)
39 sys_exit(127)
40 }
41 let st: *i64 = sys_mmap(16) as *i64
42 sys_wait4(pid, st, 0)
43 return (st[0] >> 8) & 0xff
44}
45
46func main(argc: i64, argv: *i64) -> i64 {
47 var store: *u8 = ECOMAT_STORE
48 var autolog: *u8 = "knowledge/status/autonomy_meter.log" as *u8
49 var queue: *u8 = "knowledge/registry/assignment_queue.tsv" as *u8
50 var lock: *u8 = "knowledge/registry/assignment_queue.tsv.lock" as *u8
51 var tlog: *u8 = "knowledge/status/ecomat_targets.log" as *u8
52 var outhtml: *u8 = "web_assets/maturity.html" as *u8
53 var beatlog: *u8 = "knowledge/status/ecomat_beat.log" as *u8
54 var mlog: *u8 = "knowledge/status/ecosystem_maturity.log" as *u8
55 var domlog: *u8 = EDL_LOG
56 if argc >= 2 { store = argv[1] as *u8 }
57 if argc >= 3 { autolog = argv[2] as *u8 }
58 if argc >= 4 { queue = argv[3] as *u8 }
59 if argc >= 5 { lock = argv[4] as *u8 }
60 if argc >= 6 { tlog = argv[5] as *u8 }
61 if argc >= 7 { outhtml = argv[6] as *u8 }
62 if argc >= 8 { beatlog = argv[7] as *u8 }
63 if argc >= 9 { mlog = argv[8] as *u8 }
64 if argc >= 10 { domlog = argv[9] as *u8 }
65 _p("=== NISHI ECOMAT FLYWHEEL BEAT: refresh-live-truth -> measure -> target -> publish, one turn ===\n" as *u8)
66 // 0. REFRESH LIVE TRUTH: probe the real sites FIRST so the maturity grade reflects reality, not stale logs
67 // (this is what makes the dashboard honest -- a down site shows up as a live-probe CONFLICT next).
68 eb_run("nx_site_liveness" as *u8)
69 // 1. MEASURE (grades live-derived where a live source exists)
70 let out: *i64 = sys_mmap(256)
71 let verdict: i64 = em_rollup_store(store, autolog, out, 0)
72 if out[2] == 0 { _p("BEAT verdict=RED reason=no-store (run nx_ecomat_seed)\n" as *u8); sys_exit(101); return 101 }
73 let mfd: i64 = sys_openat_append(mlog, 0x1a4)
74 if mfd >= 0 { _fp(mfd, "ECOMAT epoch=" as *u8); _fn(mfd, sys_now_realtime_sec()); _fp(mfd, " domains=" as *u8); _fn(mfd, out[2]); _fp(mfd, " overall_permil=" as *u8); _fn(mfd, out[0]); _fp(mfd, " autonomy_permil=" as *u8); _fn(mfd, out[5]); _fp(mfd, " liar_kill=" as *u8); _fn(mfd, out[1]); _fp(mfd, " src=beat verdict=" as *u8); if verdict == 0 { _fp(mfd, "GREEN" as *u8) } else { _fp(mfd, "RED" as *u8) } _fp(mfd, "\n" as *u8); sys_close(mfd) }
75 // 1b. PER-DOMAIN SNAPSHOT -- record WHICH domain held which level on this beat. Without it the
76 // aggregate line written above is undecomposable forever after: a rung CLIMBED and a domain
77 // DECLARED are indistinguishable inside a total, which is why attribution over the historical
78 // ledger tops out at 406 permil. One snapshot per beat makes every future span exact (1000).
79 // Additive by construction: its own log, its own organ's lib, the rollup path untouched.
80 // âš The aggregate ECOMAT line above and these per-domain rows come from TWO SEPARATE walks of the
81 // same store, and em_domain_level reads evidence logs off disk that sibling gates rewrite on
82 // their own beats. If the store moves between the two walks we would emit an aggregate and a
83 // per-domain snapshot that DISAGREE -- and those two files are exactly the inputs the trend and
84 // domtrend analyses difference, so the disagreement would resurface later as fabricated
85 // movement. So: dry-walk FIRST, prove it still matches the aggregate we just wrote, and only
86 // then open the log. Skipping one beat LOUDLY beats logging a beat that quietly contradicts
87 // its own headline.
88 let dsums: *i64 = sys_mmap(EDL_SUMS * 8) as *i64
89 let dnow: i64 = sys_now_realtime_sec()
90 let dpre: i64 = edl_walk(store, 0 - 1, 0, dsums)
91 var dok: i64 = 0
92 if dpre == out[2] { if dsums[0] == out[6] { if dsums[1] == out[7] { dok = 1 } } }
93 if dok == 1 {
94 let dfd: i64 = sys_openat_append(domlog, 0x1a4)
95 if dfd >= 0 {
96 let dn: i64 = edl_walk(store, dfd, dnow, dsums)
97 edl_end(dfd, dnow, dn, dsums)
98 sys_close(dfd)
99 }
100 } else {
101 _p(" [1b] per-domain snapshot SKIPPED -- store drifted mid-beat (walk " as *u8); _fn(1, dpre)
102 _p("/" as *u8); _fn(1, dsums[0]); _p("/" as *u8); _fn(1, dsums[1])
103 _p(" vs aggregate " as *u8); _fn(1, out[2]); _p("/" as *u8); _fn(1, out[6]); _p("/" as *u8); _fn(1, out[7])
104 _p("); no row written rather than a contradictory one\n" as *u8)
105 }
106 // 2. TARGET (feed the loop's queue, idempotent, lock-protected)
107 let szp: *i64 = sys_mmap(16) as *i64
108 let qb: *u8 = ss_readall(queue, szp)
109 var qn: i64 = szp[0]
110 if qn < 0 { qn = 0 }
111 let lk: i64 = rt_lock(lock)
112 let qf: i64 = sys_openat_append(queue, 0x1a4)
113 let lf: i64 = sys_openat_append(tlog, 0x1a4)
114 let tout: *i64 = sys_mmap(32)
115 et_emit_targets_core(store, qb, qn, qf, lf, tout)
116 if lf >= 0 { sys_close(lf) }
117 if qf >= 0 { sys_close(qf) }
118 rt_unlock(lock, lk)
119 // 3. PUBLISH (re-emit the dashboard from the live store)
120 let pv: i64 = em_emit_page(store, autolog, outhtml)
121 // 4. BEAT LEDGER
122 let bfd: i64 = sys_openat_append(beatlog, 0x1a4)
123 if bfd >= 0 { _fp(bfd, "ECOMAT-BEAT epoch=" as *u8); _fn(bfd, sys_now_realtime_sec()); _fp(bfd, " overall_permil=" as *u8); _fn(bfd, out[0]); _fp(bfd, " autonomy_permil=" as *u8); _fn(bfd, out[5]); _fp(bfd, " domains=" as *u8); _fn(bfd, out[2]); _fp(bfd, " targets_emitted=" as *u8); _fn(bfd, tout[0]); _fp(bfd, " done_marks=" as *u8); _fn(bfd, tout[1]); _fp(bfd, " page=" as *u8); if pv >= 0 { _fp(bfd, "written" as *u8) } else { _fp(bfd, "FAIL" as *u8) } _fp(bfd, " verdict=" as *u8); if verdict == 0 { _fp(bfd, "GREEN" as *u8) } else { _fp(bfd, "RED" as *u8) } _fp(bfd, "\n" as *u8); sys_close(bfd) }
124 _p("BEAT overall=" as *u8); _fn(1, out[0]); _p(" permil autonomy=" as *u8); _fn(1, out[5]); _p(" permil targets+=" as *u8); _fn(1, tout[0]); _p(" page=" as *u8)
125 if pv >= 0 { _p("written" as *u8) } else { _p("FAIL" as *u8) }
126 _p(" verdict=" as *u8)
127 if verdict == 0 { _p("GREEN" as *u8) } else { _p("RED" as *u8) }
128 _p(" (durable ecomat_beat.log; wire into nx_conductor_live tp_tail = autonomous flywheel)\n" as *u8)
129 sys_exit(verdict)
130 return verdict
131}