code wiki / _hdl_build / nx_ecomat_beat.nx
nx_ecomat_beat.nx source
↩ module page · 138 lines · 8645 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 var pclog: *u8 = "knowledge/status/ecomat_plancov.log" as *u8
66 if argc >= 11 { pclog = argv[10] as *u8 }
67 _p("=== NISHI ECOMAT FLYWHEEL BEAT: refresh-live-truth -> measure -> target -> publish, one turn ===\n" as *u8)
68 // 0. REFRESH LIVE TRUTH: probe the real sites FIRST so the maturity grade reflects reality, not stale logs
69 // (this is what makes the dashboard honest -- a down site shows up as a live-probe CONFLICT next).
70 eb_run("nx_site_liveness" as *u8)
71 // 1. MEASURE (grades live-derived where a live source exists)
72 let out: *i64 = sys_mmap(256)
73 let verdict: i64 = em_rollup_store(store, autolog, out, 0)
74 if out[2] == 0 { _p("BEAT verdict=RED reason=no-store (run nx_ecomat_seed)\n" as *u8); sys_exit(101); return 101 }
75 let mfd: i64 = sys_openat_append(mlog, 0x1a4)
76 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); em_axes_known_guard(mfd, out[5], out[14], out[15]); _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) }
77 // 1b. PER-DOMAIN SNAPSHOT -- record WHICH domain held which level on this beat. Without it the
78 // aggregate line written above is undecomposable forever after: a rung CLIMBED and a domain
79 // DECLARED are indistinguishable inside a total, which is why attribution over the historical
80 // ledger tops out at 406 permil. One snapshot per beat makes every future span exact (1000).
81 // Additive by construction: its own log, its own organ's lib, the rollup path untouched.
82 // âš The aggregate ECOMAT line above and these per-domain rows come from TWO SEPARATE walks of the
83 // same store, and em_domain_level reads evidence logs off disk that sibling gates rewrite on
84 // their own beats. If the store moves between the two walks we would emit an aggregate and a
85 // per-domain snapshot that DISAGREE -- and those two files are exactly the inputs the trend and
86 // domtrend analyses difference, so the disagreement would resurface later as fabricated
87 // movement. So: dry-walk FIRST, prove it still matches the aggregate we just wrote, and only
88 // then open the log. Skipping one beat LOUDLY beats logging a beat that quietly contradicts
89 // its own headline.
90 let dsums: *i64 = sys_mmap(EDL_SUMS * 8) as *i64
91 let dnow: i64 = sys_now_realtime_sec()
92 let dpre: i64 = edl_walk(store, 0 - 1, 0, dsums)
93 var dok: i64 = 0
94 if dpre == out[2] { if dsums[0] == out[6] { if dsums[1] == out[7] { dok = 1 } } }
95 if dok == 1 {
96 let dfd: i64 = sys_openat_append(domlog, 0x1a4)
97 if dfd >= 0 {
98 let dn: i64 = edl_walk(store, dfd, dnow, dsums)
99 edl_end(dfd, dnow, dn, dsums)
100 sys_close(dfd)
101 }
102 } else {
103 _p(" [1b] per-domain snapshot SKIPPED -- store drifted mid-beat (walk " as *u8); _fn(1, dpre)
104 _p("/" as *u8); _fn(1, dsums[0]); _p("/" as *u8); _fn(1, dsums[1])
105 _p(" vs aggregate " as *u8); _fn(1, out[2]); _p("/" as *u8); _fn(1, out[6]); _p("/" as *u8); _fn(1, out[7])
106 _p("); no row written rather than a contradictory one\n" as *u8)
107 }
108 // 2. TARGET (feed the loop's queue, idempotent, lock-protected)
109 let szp: *i64 = sys_mmap(16) as *i64
110 let qb: *u8 = ss_readall(queue, szp)
111 var qn: i64 = szp[0]
112 if qn < 0 { qn = 0 }
113 let lk: i64 = rt_lock(lock)
114 let qf: i64 = sys_openat_append(queue, 0x1a4)
115 let lf: i64 = sys_openat_append(tlog, 0x1a4)
116 let tout: *i64 = sys_mmap(32)
117 et_emit_targets_core(store, qb, qn, qf, lf, tout)
118 if lf >= 0 { sys_close(lf) }
119 if qf >= 0 { sys_close(qf) }
120 rt_unlock(lock, lk)
121 // 2b. EC2: PLAN COVERAGE AS A PUBLISHED ROW, every beat. The measurement was taken by hand once on
122 // 2026-08-21 (70 domains, 63 planned, 7 not) and then lived in a transcript -- folklore by the next
123 // session. Now the beat emits the partition whether anyone asks or not, names every planless and
124 // ladderless domain, and attributes a ranking refusal to the MISSING PLAN, never to the ranker.
125 em_plan_coverage(pclog)
126 // 3. PUBLISH (re-emit the dashboard from the live store)
127 let pv: i64 = em_emit_page(store, autolog, outhtml)
128 // 4. BEAT LEDGER
129 let bfd: i64 = sys_openat_append(beatlog, 0x1a4)
130 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); em_axes_known_guard(bfd, out[5], out[14], out[15]); _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) }
131 _p("BEAT overall=" as *u8); _fn(1, out[0]); _p(" permil autonomy=" as *u8); em_axes_known_guard(1, out[5], out[14], out[15]); _p(" permil targets+=" as *u8); _fn(1, tout[0]); _p(" page=" as *u8)
132 if pv >= 0 { _p("written" as *u8) } else { _p("FAIL" as *u8) }
133 _p(" verdict=" as *u8)
134 if verdict == 0 { _p("GREEN" as *u8) } else { _p("RED" as *u8) }
135 _p(" (durable ecomat_beat.log; wire into nx_conductor_live tp_tail = autonomous flywheel)\n" as *u8)
136 sys_exit(verdict)
137 return verdict
138}