code wiki / _hdl_build / nx_ecomat_lib.nx
nx_ecomat_lib.nx source
↩ module page · 577 lines · 32789 B
1// nx_ecomat_lib.nx -- shared core for the ECOSYSTEM MATURITY ROLLUP (the unifier), SOVEREIGN edition.
2// Operator: "get away from tsv and other 3rd party formats -- use the nishi ecosystem, get to S-class."
3// So the maturity chart lives in the NATIVE seg_store (knowledge/store/ecomat) as BINARY records (the
4// store's own ss_w32/ss_r32 codec -- no TSV, no delimiter format), and the rollup MEASURES from the
5// store + the live autonomy ledger, enforcing a LIAR-KILL (no S-CLASS claim without evidence).
6// Pairs with nx_seg_store (the sovereign store) + nx_maturity_auditor (the ladder). NO FLOAT.
7// RECORD layout (one value per domain, key "ecomat:dom:N"):
8// w32@0 axis(0=DEPTH 1=BREADTH 2=META) | @4 layer | @8 cur | @12 bar | @16 weight | @20 ev
9// @24: domain\0 bench\0 next_rung\0
10// license_tier: ORIGINAL
11import "nx_seg_store.nx"
12import "nx_syscalls.nx"
13import "nx_maturity_auditor.nx"
14
15const ECOMAT_STORE: *u8 = "knowledge/store/ecomat"
16
17// Byte offset where the record's 7 NUL-terminated strings begin (the w32 header occupies 0..31).
18// Named, not magic: it IS the record layout documented above ec_pack.
19const ECOMAT_REC_STRBASE: i64 = 32
20
21// Read-side scratch sizes for ec_str. Named so the ALLOCATION and the BOUND are the same symbol and
22// can never drift apart -- the drift is exactly how an unbounded copy turns into a silent overrun.
23const ECOMAT_DOM_CAP: i64 = 128
24const ECOMAT_NR_CAP: i64 = 160
25const ECOMAT_EVLOG_CAP: i64 = 256
26const ECOMAT_EVPAT_CAP: i64 = 64
27
28func el_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
29func _p(s: *u8) -> i64 { let n: i64=el_len(s); sys_write(1,s,n); return 0 }
30func _fp(fd: i64, s: *u8) -> i64 { let n: i64=el_len(s); sys_write(fd,s,n); return 0 }
31func _fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); 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{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
32func el_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 }
33func el_match(buf: *u8, i: i64, pat: *u8, pl: i64) -> i64 { var k: i64=0; while k<pl { if buf[i+k]!=pat[k] { return 0 } k=k+1 } return 1 }
34
35// value of the LAST integer following pat in buf (e.g. "permil=") -- reads the live ledger, -1 if none.
36func el_last_after(buf: *u8, n: i64, pat: *u8) -> i64 {
37 let pl: i64 = el_len(pat)
38 var res: i64 = 0 - 1
39 var i: i64 = 0
40 while i + pl <= n {
41 if el_match(buf, i, pat, pl) == 1 {
42 var j: i64 = i + pl
43 var v: i64 = 0
44 var any: i64 = 0
45 var go: i64 = 1
46 while go == 1 {
47 if j >= n { go = 0 } else {
48 let c: i64 = buf[j] as i64
49 var d: i64 = 0
50 if c >= 48 { if c <= 57 { d = 1 } }
51 if d == 1 { v = v*10 + (c-48); j = j + 1; any = 1 } else { go = 0 }
52 }
53 }
54 if any == 1 { res = v }
55 }
56 i = i + 1
57 }
58 return res
59}
60
61// gate-liveness: is the LAST occurrence of pat (e.g. "verdict=") followed by "GREEN"? (handles RED-then-GREEN logs)
62func el_last_green(buf: *u8, n: i64, pat: *u8) -> i64 {
63 let pl: i64 = el_len(pat)
64 var res: i64 = 0
65 var i: i64 = 0
66 while i + pl <= n {
67 if el_match(buf, i, pat, pl) == 1 {
68 var ok: i64 = 0
69 if el_match(buf, i + pl, "GREEN" as *u8, 5) == 1 { ok = 1 }
70 if el_match(buf, i + pl, "PASS" as *u8, 4) == 1 { ok = 1 }
71 if el_match(buf, i + pl, "VALID" as *u8, 5) == 1 { ok = 1 }
72 res = ok
73 }
74 i = i + 1
75 }
76 return res
77}
78
79// ---- native seg_store record codec (no TSV) ----
80func ec_axis_label(a: i64) -> *u8 { if a==0 { return "DEPTH" as *u8 } if a==1 { return "BREADTH" as *u8 } return "META" as *u8 }
81func em_tri_label(t: i64) -> *u8 { if t==2 { return "[triangulated]" as *u8 } if t==3 { return "[CONFLICT]" as *u8 } if t==1 { return "[single-src]" as *u8 } if t==4 { return "[dangling]" as *u8 } return "[stored]" as *u8 }
82
83// key "ecomat:dom:N" into out
84func ec_key(k: i64, out: *u8) -> i64 {
85 let p: *u8 = "ecomat:dom:" as *u8
86 var o: i64 = 0
87 var i: i64 = 0
88 while p[i] != (0 as u8) { out[o]=p[i]; o=o+1; i=i+1 }
89 if k == 0 { out[o]=48 as u8; o=o+1 } else { let t: *u8=sys_mmap(24); var m: i64=k; var n: i64=0; while m>0 { t[n]=(48+(m%10)) as u8; m=m/10; n=n+1 } var j: i64=0; while j<n { out[o]=t[n-1-j]; o=o+1; j=j+1 } }
90 out[o]=0 as u8; return o
91}
92
93// BOUNDED append: returns the new offset, or -1 if the string plus its NUL would pass cap.
94// Fail-closed and PROPAGATING -- a negative offset in feeds a negative offset out, so one refusal
95// aborts the whole ec_pack chain instead of letting a half-written record reach the store.
96func ec_putstr(buf: *u8, cap: i64, off: i64, s: *u8) -> i64 {
97 if off < 0 { return 0 - 1 }
98 let n: i64 = el_len(s)
99 if off + n + 1 > cap { return 0 - 1 }
100 var o: i64 = off
101 var i: i64 = 0
102 while i < n { buf[o]=s[i]; o=o+1; i=i+1 }
103 buf[o]=0 as u8
104 return o + 1
105}
106
107// EXACT bytes ec_pack will write for these fields. Callers size the allocation from the DATA
108// instead of guessing a fixed cap, so an oversized row is impossible BY CONSTRUCTION rather than
109// rejected after the fact -- this is what let the lab-science domain die on a bogus "record too
110// large" while the seeder's 512-byte buffers were silently overrun.
111func ec_reclen(domain: *u8, bench: *u8, nextrung: *u8, evlog: *u8, evpat: *u8, evlog2: *u8, evpat2: *u8) -> i64 {
112 var n: i64 = ECOMAT_REC_STRBASE
113 n = n + el_len(domain) + 1
114 n = n + el_len(bench) + 1
115 n = n + el_len(nextrung) + 1
116 n = n + el_len(evlog) + 1
117 n = n + el_len(evpat) + 1
118 n = n + el_len(evlog2) + 1
119 n = n + el_len(evpat2) + 1
120 return n
121}
122
123// pack a record into buf; return total byte length.
124// record: w32 @0 axis @4 layer @8 cur @12 bar @16 weight @20 ev @24 evkind @28 evkind2 ; strings @32
125// evkind/evkind2: 0=none 1=permil-derive 2=gate-liveness. TWO INDEPENDENT sources -> TRIANGULATION: a grade is
126// trusted only when its sources CONVERGE (<=1 level apart); divergence = CONFLICT (flagged); one source = SINGLE
127// (un-triangulated, flagged). The consensus is the conservative MIN -- never overclaim past the weaker witness.
128// cap = the writable size of buf. Returns the packed length, or -1 if buf cannot hold the record
129// (no partial write ever lands). Size buf with ec_reclen and this can only fire on a caller bug.
130func ec_pack(buf: *u8, cap: i64, axis: i64, layer: i64, cur: i64, bar: i64, weight: i64, ev: i64, evkind: i64, evkind2: i64, domain: *u8, bench: *u8, nextrung: *u8, evlog: *u8, evpat: *u8, evlog2: *u8, evpat2: *u8) -> i64 {
131 if cap < ECOMAT_REC_STRBASE { return 0 - 1 }
132 ss_w32(buf, 0, axis); ss_w32(buf, 4, layer); ss_w32(buf, 8, cur); ss_w32(buf, 12, bar); ss_w32(buf, 16, weight); ss_w32(buf, 20, ev); ss_w32(buf, 24, evkind); ss_w32(buf, 28, evkind2)
133 var o: i64 = ECOMAT_REC_STRBASE
134 o = ec_putstr(buf, cap, o, domain)
135 o = ec_putstr(buf, cap, o, bench)
136 o = ec_putstr(buf, cap, o, nextrung)
137 o = ec_putstr(buf, cap, o, evlog)
138 o = ec_putstr(buf, cap, o, evpat)
139 o = ec_putstr(buf, cap, o, evlog2)
140 o = ec_putstr(buf, cap, o, evpat2)
141 return o
142}
143func ec_axis(v: *u8) -> i64 { return ss_r32(v, 0) }
144func ec_layer(v: *u8) -> i64 { return ss_r32(v, 4) }
145func ec_cur(v: *u8) -> i64 { return ss_r32(v, 8) }
146func ec_bar(v: *u8) -> i64 { return ss_r32(v, 12) }
147func ec_weight(v: *u8) -> i64 { return ss_r32(v, 16) }
148func ec_ev(v: *u8) -> i64 { return ss_r32(v, 20) }
149func ec_evkind(v: *u8) -> i64 { return ss_r32(v, 24) }
150func ec_evkind2(v: *u8) -> i64 { return ss_r32(v, 28) }
151// extract string field: which 0=domain 1=bench 2=next_rung 3=evlog 4=evpat 5=evlog2 6=evpat2
152// cap = writable size of out. Returns the field length, or -1 if it will not fit -- and on refusal
153// out is set EMPTY, never partially filled, so a caller that ignores the return reads "" rather than
154// a truncated-but-plausible domain name. Mirrors db_dec_str(g,out,cap); ec_str was the outlier that
155// copied a stored field into a fixed buffer with no idea how big that buffer was.
156func ec_str(v: *u8, which: i64, out: *u8, cap: i64) -> i64 {
157 if cap < 1 { return 0 - 1 }
158 out[0] = 0 as u8
159 var off: i64 = ECOMAT_REC_STRBASE
160 var idx: i64 = 0
161 while idx < which { while v[off]!=(0 as u8){ off=off+1 } off=off+1; idx=idx+1 }
162 var n: i64 = 0
163 while v[off+n]!=(0 as u8){ n=n+1 }
164 if n + 1 > cap { return 0 - 1 }
165 var o: i64 = 0
166 while o < n { out[o]=v[off+o]; o=o+1 }
167 out[o]=0 as u8; return o
168}
169// STORED mode (no live source) -- used by the gates.
170func ec_seed_one(w: *i64, idx: i64, axis: i64, layer: i64, cur: i64, bar: i64, weight: i64, ev: i64, domain: *u8, bench: *u8, nextrung: *u8) -> i64 {
171 let key: *u8 = sys_mmap(64); ec_key(idx, key)
172 let cap: i64 = ec_reclen(domain, bench, nextrung, "" as *u8, "" as *u8, "" as *u8, "" as *u8)
173 let val: *u8 = sys_mmap(cap); let vlen: i64 = ec_pack(val, cap, axis, layer, cur, bar, weight, ev, 0, 0, domain, bench, nextrung, "" as *u8, "" as *u8, "" as *u8, "" as *u8)
174 if vlen < 0 { return 0 - 1 }
175 if ss_add(w, 1, key, val, vlen) != 0 { return 0 - 1 }
176 return 0
177}
178// SINGLE live source (un-triangulated).
179func ec_seed_one_ev(w: *i64, idx: i64, axis: i64, layer: i64, cur: i64, bar: i64, weight: i64, ev: i64, evkind: i64, domain: *u8, bench: *u8, nextrung: *u8, evlog: *u8, evpat: *u8) -> i64 {
180 let key: *u8 = sys_mmap(64); ec_key(idx, key)
181 let cap: i64 = ec_reclen(domain, bench, nextrung, evlog, evpat, "" as *u8, "" as *u8)
182 let val: *u8 = sys_mmap(cap); let vlen: i64 = ec_pack(val, cap, axis, layer, cur, bar, weight, ev, evkind, 0, domain, bench, nextrung, evlog, evpat, "" as *u8, "" as *u8)
183 if vlen < 0 { return 0 - 1 }
184 if ss_add(w, 1, key, val, vlen) != 0 { return 0 - 1 }
185 return 0
186}
187// TWO independent live sources -> the rollup TRIANGULATES (checks convergence).
188func ec_seed_two(w: *i64, idx: i64, axis: i64, layer: i64, cur: i64, bar: i64, weight: i64, ev: i64, evkind: i64, evkind2: i64, domain: *u8, bench: *u8, nextrung: *u8, evlog: *u8, evpat: *u8, evlog2: *u8, evpat2: *u8) -> i64 {
189 let key: *u8 = sys_mmap(64); ec_key(idx, key)
190 let cap: i64 = ec_reclen(domain, bench, nextrung, evlog, evpat, evlog2, evpat2)
191 let val: *u8 = sys_mmap(cap); let vlen: i64 = ec_pack(val, cap, axis, layer, cur, bar, weight, ev, evkind, evkind2, domain, bench, nextrung, evlog, evpat, evlog2, evpat2)
192 if vlen < 0 { return 0 - 1 }
193 if ss_add(w, 1, key, val, vlen) != 0 { return 0 - 1 }
194 return 0
195}
196
197// permil (0..1000) -> maturity level (0..5): live-derive a grade from a live meter so it can't rot.
198func permil_to_level(p: i64) -> i64 {
199 // â›”A COVERAGE PERCENTAGE MUST NEVER REACH A RUNG THAT NAMES AN EVIDENCE PROPERTY.
200 // nx_maturity_auditor defines MAT_SCLASS(4) as TRIANGULATED PARITY vs a named best-in-class
201 // competitor and MAT_EXCEED(5) as a triangulated WIN. Those are CLAIMS ABOUT A COMPARISON, not
202 // capability tiers. This function used to award them from a permil (p>=700 -> 4, p>=900 -> 5),
203 // so a domain reporting high COVERAGE was stamped "parity with gcc/llvm" WITH NO COMPARISON
204 // HAVING RUN -- the laundering path behind cov=1000 rows sitting in CLAIM-ONLY while
205 // nx_sota_status reports PROVEN 0/40. Ceiling is now PRODUCTION(3); 4 and 5 require an explicit
206 // comparator row (competitor + method + verdict), never a number.
207 // INERT ON ADOPTION: the rollup reported sclass_plus=0, so no domain held 4 or 5 when this
208 // landed -- it removes a FUTURE inflation path, it does not restate any current grade.
209 if p >= 500 { return 3 }
210 if p >= 150 { return 2 }
211 return 1
212}
213
214// derive a maturity level from ONE evidence source. THREE distinct outcomes (the split is load-bearing):
215// 0..5 = a real MEASURED level (the source was readable and gave an answer)
216// -1 = NO source declared (evkind 0) -- fall back to the stored assertion
217// -2 = source DECLARED but DANGLING (file missing/unreadable, or evkind-1 pattern absent) -- we CANNOT measure
218// The -2 vs (1) distinction is the whole point: a RED gate (file PRESENT, verdict not GREEN) is a real
219// measurement of TOY(1); a MISSING gate is the ABSENCE of measurement (-2). Conflating them (the old bug)
220// let a dead evidence pointer read as a low-but-"measured" grade, and worse, two missing gates "converged"
221// at TOY and got stamped [triangulated] -- a validation claim with zero readable sources behind it.
222// evkind 1 = permil-derive (evlog's evpat reading -> level); 2 = gate-liveness (last evpat verdict GREEN/PASS
223// -> the stored level is live-confirmed, else PRESENT-but-not-GREEN = RED = demote to TOY).
224func em_derive_level(evkind: i64, evlog: *u8, evpat: *u8, stored: i64) -> i64 {
225 if evkind == 0 { return 0 - 1 }
226 let szp: *i64 = sys_mmap(16) as *i64
227 let eb: *u8 = ss_readall(evlog, szp)
228 let en: i64 = szp[0]
229 // en == 0 is an EMPTY log: the file exists but carries no verdict. That is the ABSENCE of a
230 // measurement, not a RED one, so it must be DANGLING like a missing file -- otherwise a gate
231 // that opened its log and died before writing scores as a real TOY(1), and two such logs
232 // "converge" at TOY and get stamped [triangulated]: a validation claim with zero readable
233 // sources, which is the exact bug this split was introduced to kill.
234 if en <= 0 { return 0 - 2 }
235 if evkind == 1 {
236 let p: i64 = el_last_after(eb, en, evpat)
237 if p >= 0 { return permil_to_level(p) }
238 return 0 - 2
239 }
240 if evkind == 2 {
241 let g: i64 = el_last_green(eb, en, evpat)
242 if g == 1 {
243 // â›”A GREEN GATE PROVES THE CAPABILITY RUNS. IT SAYS NOTHING ABOUT A COMPETITOR.
244 // MAT_SCLASS(4) and MAT_EXCEED(5) are defined as TRIANGULATED PARITY / WIN vs a NAMED
245 // best-in-class. Gate-liveness is CAPABILITY evidence, so it can confirm at most
246 // PRODUCTION(3). Letting a green gate carry a stored 4/5 through was the LAST path to
247 // an unearned comparative rung, after permil_to_level was capped the same day.
248 // Comparative rungs require an ADMITTED claim (nx_vsbest_lib: named competitor +
249 // re-runnable method + readable evidence containing the declared pattern).
250 // INERT ON ADOPTION: the rollup reported sclass_plus=0, so nothing is demoted by this;
251 // it closes a FUTURE inflation path rather than restating any current grade.
252 if stored > MAT_PRODUCTION { return MAT_PRODUCTION }
253 return stored
254 }
255 return 1
256 }
257 return 0 - 1
258}
259
260// THE single source of truth for a domain's live grade + validation status -- used by BOTH the rollup
261// AND the page so they can never diverge. tout[0]: 0 stored / 1 single-src / 2 triangulated / 3 CONFLICT /
262// 4 DANGLING (a source was DECLARED but no witness is currently readable -> UNVERIFIED, never [triangulated]).
263// Two REAL measured witnesses -> conservative MIN consensus; convergence (<=1 apart) = triangulated, else conflict.
264// One readable witness -> single-src (the OTHER being absent OR dangling must NOT discard the readable one --
265// the old code fell straight to stored whenever witness-1 was unreadable, throwing away a live witness-2).
266func em_domain_level(v: *u8, tout: *i64) -> i64 {
267 let stored: i64 = ec_cur(v)
268 let evk1: i64 = ec_evkind(v)
269 let evk2: i64 = ec_evkind2(v)
270 let elog: *u8 = sys_mmap(ECOMAT_EVLOG_CAP)
271 let epat: *u8 = sys_mmap(ECOMAT_EVPAT_CAP)
272 let elog2: *u8 = sys_mmap(ECOMAT_EVLOG_CAP)
273 let epat2: *u8 = sys_mmap(ECOMAT_EVPAT_CAP)
274 ec_str(v, 3, elog, ECOMAT_EVLOG_CAP); ec_str(v, 4, epat, ECOMAT_EVPAT_CAP); ec_str(v, 5, elog2, ECOMAT_EVLOG_CAP); ec_str(v, 6, epat2, ECOMAT_EVPAT_CAP)
275 let l1: i64 = em_derive_level(evk1, elog, epat, stored)
276 let l2: i64 = em_derive_level(evk2, elog2, epat2, stored)
277 let DANG: i64 = 0 - 2
278 if l1 >= 0 {
279 if l2 >= 0 {
280 var diff: i64 = l1 - l2
281 if diff < 0 { diff = 0 - diff }
282 var lo: i64 = l1
283 if l2 < lo { lo = l2 }
284 if diff <= 1 { tout[0] = 2 } else { tout[0] = 3 }
285 return lo
286 }
287 tout[0] = 1
288 return l1
289 }
290 if l2 >= 0 {
291 tout[0] = 1
292 return l2
293 }
294 // NEITHER witness readable. A DECLARED-but-dangling pointer is UNVERIFIED (show the stored assertion,
295 // flagged [dangling], counted apart, and -- via the rollup -- ev=0 so a high claim still liar-kills).
296 // No pointer declared at all -> the honest stored baseline.
297 if l1 == DANG { tout[0] = 4; return stored }
298 if l2 == DANG { tout[0] = 4; return stored }
299 tout[0] = 0
300 return stored
301}
302
303// ---- the measured core: MEASURE from the store + the live autonomy ledger ----
304// out[0]=overall_permil out[1]=liar_kill out[2]=domains out[3]=sclass_plus
305// out[4]=absent out[5]=auto_permil out[6]=sum_cur out[7]=sum_bar ; returns 0 GREEN / 1 RED.
306func em_rollup_store(prefix: *u8, autolog: *u8, out: *i64, verbose: i64) -> i64 {
307 let szp: *i64 = sys_mmap(16) as *i64
308 let abuf: *u8 = ss_readall(autolog, szp)
309 var an: i64 = szp[0]
310 if an < 0 { an = 0 }
311 var auto_permil: i64 = 0 - 1
312 if an > 0 { auto_permil = el_last_after(abuf, an, "permil=" as *u8) }
313 let h: *i64 = ss_open(prefix)
314 var sum_cur: i64 = 0
315 var sum_bar: i64 = 0
316 var domains: i64 = 0
317 var bad: i64 = 0
318 var sclass_plus: i64 = 0
319 var absent: i64 = 0
320 var triangulated: i64 = 0
321 var conflict: i64 = 0
322 var single: i64 = 0
323 var dangling: i64 = 0
324 let pq: *i64 = sys_mmap(16) as *i64
325 let lq: *i64 = sys_mmap(16) as *i64
326 let dom: *u8 = sys_mmap(ECOMAT_DOM_CAP)
327 let nr: *u8 = sys_mmap(ECOMAT_NR_CAP)
328 let elog: *u8 = sys_mmap(ECOMAT_EVLOG_CAP)
329 let epat: *u8 = sys_mmap(ECOMAT_EVPAT_CAP)
330 let elog2: *u8 = sys_mmap(ECOMAT_EVLOG_CAP)
331 let epat2: *u8 = sys_mmap(ECOMAT_EVPAT_CAP)
332 let szp2: *i64 = sys_mmap(16) as *i64
333 if (h as i64) != 0 {
334 var k: i64 = 0
335 var go: i64 = 1
336 while go == 1 {
337 let key: *u8 = sys_mmap(64)
338 ec_key(k, key)
339 if ss_hget(h, key, pq, lq) == 1 {
340 let v: *u8 = pq[0] as *u8
341 ec_str(v, 0, dom, ECOMAT_DOM_CAP)
342 var cur: i64 = ec_cur(v)
343 let bar: i64 = ec_bar(v)
344 var ev: i64 = ec_ev(v)
345 // LIVE-DERIVE per the record's own evidence pointer (data-driven, anti-staleness): a grade
346 // is only what its live source currently supports. evkind 1 = permil-derive the LEVEL from
347 // evlog's evpat reading; 2 = gate-liveness (level holds while the gate is GREEN, else demote).
348 // TRIANGULATE via the shared em_domain_level (same logic the page uses, so they can't diverge).
349 let tout: *i64 = sys_mmap(16) as *i64
350 let dl: i64 = em_domain_level(v, tout)
351 let tstat: i64 = tout[0]
352 // any derived status overrides the stored LEVEL; the EVIDENCE flag is set only when a witness
353 // was actually READABLE (single/tri/conflict). DANGLING keeps ev=0 -> a high claim behind a
354 // dead pointer still trips the liar-kill instead of hiding as a silently-demoted TOY.
355 if tstat != 0 { cur = dl }
356 if tstat == 1 { single = single + 1; ev = 1 }
357 if tstat == 2 { triangulated = triangulated + 1; ev = 1 }
358 if tstat == 3 { conflict = conflict + 1; ev = 1 }
359 if tstat == 4 { dangling = dangling + 1; ev = 0 }
360 var viol: i64 = 0
361 if cur >= MAT_SCLASS { if ev == 0 { viol = 1 } }
362 if viol == 1 { bad = bad + 1 }
363 sum_cur = sum_cur + cur
364 sum_bar = sum_bar + bar
365 domains = domains + 1
366 if cur >= MAT_SCLASS { sclass_plus = sclass_plus + 1 }
367 if cur == 0 { absent = absent + 1 }
368 if verbose == 1 {
369 ec_str(v, 0, dom, ECOMAT_DOM_CAP); ec_str(v, 2, nr, ECOMAT_NR_CAP)
370 let lc: *u8 = mat_label(cur)
371 let lb: *u8 = mat_label(bar)
372 let ax: *u8 = ec_axis_label(ec_axis(v))
373 _p(" " as *u8); _p(ax); _p(" / " as *u8); _p(dom); _p(" [" as *u8); _p(lc); _p(" -> " as *u8); _p(lb); _p("] " as *u8)
374 let tl: *u8 = em_tri_label(tstat); _p(tl); _p(" next: " as *u8); _p(nr)
375 if viol == 1 { _p(" <== LIAR-KILL: S-CLASS claim, no evidence" as *u8) }
376 _p("\n" as *u8)
377 }
378 k = k + 1
379 } else { go = 0 }
380 }
381 }
382 var permil: i64 = 0
383 if sum_bar > 0 { permil = (1000 * sum_cur) / sum_bar }
384 out[0] = permil; out[1] = bad; out[2] = domains; out[3] = sclass_plus
385 out[4] = absent; out[5] = auto_permil; out[6] = sum_cur; out[7] = sum_bar
386 out[8] = triangulated; out[9] = conflict; out[10] = single; out[11] = dangling
387 if bad > 0 { return 1 }
388 return 0
389}
390
391// ---- TARGET EMIT core (R1): the maturity gaps BECOME ranked work for the loop ----
392func el_contains(buf: *u8, n: i64, pat: *u8) -> i64 {
393 let pl: i64 = el_len(pat)
394 if pl == 0 { return 0 }
395 var i: i64 = 0
396 while i + pl <= n { if el_match(buf, i, pat, pl) == 1 { return 1 } i = i + 1 }
397 return 0
398}
399func et_mkid(domain: *u8, out: *u8) -> i64 {
400 let pre: *u8 = "GEN-MAT-" as *u8
401 var o: i64 = 0
402 var i: i64 = 0
403 while pre[i] != (0 as u8) { out[o]=pre[i]; o=o+1; i=i+1 }
404 i = 0
405 while domain[i] != (0 as u8) { out[o]=domain[i]; o=o+1; i=i+1 }
406 out[o] = 0 as u8
407 return o
408}
409func et_should_emit(domain: *u8, cur: i64, bar: i64, qb: *u8, qn: i64) -> i64 {
410 if cur >= bar { return 0 }
411 let idp: *u8 = sys_mmap(160)
412 et_mkid(domain, idp)
413 if el_contains(qb, qn, idp) == 1 { return 0 }
414 return 1
415}
416// count the targets the emitter WOULD append, reading the STORE (gate proves this without the loop).
417func et_count_targets_store(prefix: *u8, qb: *u8, qn: i64) -> i64 {
418 // ss_open_cached (seq905/962 class fix, seq1347 migration, 2026-07-30). SAFE: open -> ss_hget loop
419 // against this same handle -> return; nothing re-enters the store while the handle is held.
420 let h: *i64 = ss_open_cached(prefix)
421 if (h as i64) == 0 { return 0 }
422 let pq: *i64 = sys_mmap(16) as *i64
423 let lq: *i64 = sys_mmap(16) as *i64
424 let dom: *u8 = sys_mmap(ECOMAT_DOM_CAP)
425 var cnt: i64 = 0
426 var k: i64 = 0
427 var go: i64 = 1
428 while go == 1 {
429 let key: *u8 = sys_mmap(64)
430 ec_key(k, key)
431 if ss_hget(h, key, pq, lq) == 1 {
432 let v: *u8 = pq[0] as *u8
433 let cur: i64 = ec_cur(v)
434 let bar: i64 = ec_bar(v)
435 ec_str(v, 0, dom, ECOMAT_DOM_CAP)
436 let se: i64 = et_should_emit(dom, cur, bar, qb, qn)
437 if se == 1 { cnt = cnt + 1 }
438 k = k + 1
439 } else { go = 0 }
440 }
441 return cnt
442}
443
444// ---- R2 PUBLISH: emit the maturity dashboard as HTML (last-mile render, organ-authored) ----
445func pg_thead(buf: *u8, off: i64) -> i64 {
446 return ss_cat(buf, off, "<tr><th>domain</th><th>level</th><th></th><th>target</th><th>next rung</th><th>w</th></tr>\n" as *u8)
447}
448// emit one <tr> per domain whose axis == want, reading the sovereign store.
449func pg_emit_axis(buf: *u8, off: i64, h: *i64, want: i64) -> i64 {
450 var o: i64 = off
451 let pq: *i64 = sys_mmap(16) as *i64
452 let lq: *i64 = sys_mmap(16) as *i64
453 let dom: *u8 = sys_mmap(ECOMAT_DOM_CAP)
454 let nr: *u8 = sys_mmap(ECOMAT_NR_CAP)
455 var k: i64 = 0
456 var go: i64 = 1
457 while go == 1 {
458 let key: *u8 = sys_mmap(64); ec_key(k, key)
459 if ss_hget(h, key, pq, lq) == 1 {
460 let v: *u8 = pq[0] as *u8
461 if ec_axis(v) == want {
462 let tout: *i64 = sys_mmap(16) as *i64
463 let cur: i64 = em_domain_level(v, tout)
464 let bar: i64 = ec_bar(v)
465 let w: i64 = ec_weight(v)
466 ec_str(v, 0, dom, ECOMAT_DOM_CAP); ec_str(v, 2, nr, ECOMAT_NR_CAP)
467 let lc: *u8 = mat_label(cur)
468 let lb: *u8 = mat_label(bar)
469 let tlab: *u8 = em_tri_label(tout[0])
470 o = ss_cat(buf, o, "<tr><td class=dom>" as *u8); o = ss_cat(buf, o, dom)
471 o = ss_cat(buf, o, "</td><td><span class='b l" as *u8); o = ss_catn(buf, o, cur)
472 o = ss_cat(buf, o, "'>" as *u8); o = ss_cat(buf, o, lc); o = ss_cat(buf, o, "</span> <span class=tri>" as *u8); o = ss_cat(buf, o, tlab); o = ss_cat(buf, o, "</span></td><td class=bar>" as *u8)
473 var s: i64 = 0
474 while s < 5 { if s < cur { o = ss_cat(buf, o, "█" as *u8) } else { o = ss_cat(buf, o, "░" as *u8) } s = s + 1 }
475 o = ss_cat(buf, o, "</td><td>→ " as *u8); o = ss_cat(buf, o, lb)
476 o = ss_cat(buf, o, "</td><td class=nx>" as *u8); o = ss_cat(buf, o, nr)
477 o = ss_cat(buf, o, "</td><td class=w>" as *u8); o = ss_catn(buf, o, w)
478 o = ss_cat(buf, o, "</td></tr>\n" as *u8)
479 }
480 k = k + 1
481 } else { go = 0 }
482 }
483 return o
484}
485// build the whole dashboard from the store + autonomy ledger, write to outhtml. Returns the
486// rollup verdict (0 GREEN, 1 RED liar-kill), or -1 if the store is missing. NO TSV, NO JS.
487func em_emit_page(prefix: *u8, autolog: *u8, outhtml: *u8) -> i64 {
488 let out: *i64 = sys_mmap(256)
489 let verdict: i64 = em_rollup_store(prefix, autolog, out, 0)
490 let h: *i64 = ss_open(prefix)
491 if (h as i64) == 0 { return 0 - 1 }
492 let buf: *u8 = sys_mmap(262144)
493 var o: i64 = 0
494 o = ss_cat(buf, o, "<!doctype html><html lang=en><head><meta charset=utf-8><meta name=viewport content='width=device-width,initial-scale=1'><title>Nishi Ecosystem Maturity</title><style>" as *u8)
495 o = ss_cat(buf, o, "body{margin:0;font:15px/1.5 -apple-system,Segoe UI,Roboto,sans-serif;background:#0e1116;color:#e6edf3}" as *u8)
496 o = ss_cat(buf, o, "header{padding:32px 24px;background:linear-gradient(135deg,#161b22,#0e1116);border-bottom:1px solid #30363d}" as *u8)
497 o = ss_cat(buf, o, "h1{margin:0 0 8px;font-size:22px}.big{font-size:54px;font-weight:800;color:#3fb950;line-height:1}" as *u8)
498 o = ss_cat(buf, o, ".sub{color:#8b949e;font-size:14px;margin-top:6px}.track{height:10px;background:#21262d;border-radius:6px;margin-top:16px;max-width:640px;overflow:hidden}" as *u8)
499 o = ss_cat(buf, o, ".fill{height:100%;background:linear-gradient(90deg,#1f6feb,#3fb950)}h2{margin:28px 24px 10px;font-size:13px;text-transform:uppercase;letter-spacing:.08em;color:#8b949e}" as *u8)
500 o = ss_cat(buf, o, "table{border-collapse:collapse;margin:0 24px 8px;max-width:980px}td,th{padding:8px 10px;border-bottom:1px solid #21262d;text-align:left;font-size:14px}" as *u8)
501 o = ss_cat(buf, o, "th{color:#8b949e;font-size:12px;text-transform:uppercase}.dom{font-weight:650}.bar{font-family:monospace;letter-spacing:2px;color:#3fb950}.nx{color:#8b949e;font-size:13px}.w{text-align:right;color:#d29922}.tri{font-size:11px;color:#6e7681}" as *u8)
502 o = ss_cat(buf, o, ".b{display:inline-block;padding:2px 8px;border-radius:10px;font-size:12px;font-weight:600}" as *u8)
503 o = ss_cat(buf, o, ".l0{background:#490202;color:#ff7b72}.l1{background:#5a1e02;color:#ffa657}.l2{background:#5c4302;color:#e3b341}.l3{background:#234d0a;color:#a5d65b}.l4{background:#0d4429;color:#3fb950}.l5{background:#0a3d62;color:#58a6ff}" as *u8)
504 o = ss_cat(buf, o, "footer{padding:20px 24px;color:#6e7681;font-size:12px;border-top:1px solid #21262d;margin-top:20px}" as *u8)
505 o = ss_cat(buf, o, "</style></head><body><header><h1>Nishi Ecosystem Maturity</h1><div class=big>" as *u8)
506 o = ss_catn(buf, o, out[0]); o = ss_cat(buf, o, "‰</div><div class=sub>toward S-class across " as *u8)
507 o = ss_catn(buf, o, out[2]); o = ss_cat(buf, o, " domains · " as *u8); o = ss_catn(buf, o, out[3])
508 o = ss_cat(buf, o, " at S-class · live autonomy " as *u8); o = ss_catn(buf, o, out[5]); o = ss_cat(buf, o, "‰ · triangulated " as *u8); o = ss_catn(buf, o, out[8]); o = ss_cat(buf, o, "/" as *u8); o = ss_catn(buf, o, out[2]); o = ss_cat(buf, o, " · conflicts " as *u8); o = ss_catn(buf, o, out[9]); o = ss_cat(buf, o, " · dangling " as *u8); o = ss_catn(buf, o, out[11]); o = ss_cat(buf, o, "</div>" as *u8)
509 let pct: i64 = out[0] / 10
510 o = ss_cat(buf, o, "<div class=track><div class=fill style='width:" as *u8); o = ss_catn(buf, o, pct); o = ss_cat(buf, o, "%'></div></div></header>" as *u8)
511 o = ss_cat(buf, o, "<h2>Depth — substrate</h2><table>" as *u8); o = pg_thead(buf, o); o = pg_emit_axis(buf, o, h, 0); o = ss_cat(buf, o, "</table>" as *u8)
512 o = ss_cat(buf, o, "<h2>Breadth — products</h2><table>" as *u8); o = pg_thead(buf, o); o = pg_emit_axis(buf, o, h, 1); o = ss_cat(buf, o, "</table>" as *u8)
513 o = ss_cat(buf, o, "<h2>Meta</h2><table>" as *u8); o = pg_thead(buf, o); o = pg_emit_axis(buf, o, h, 2); o = ss_cat(buf, o, "</table>" as *u8)
514 o = ss_cat(buf, o, "<footer>sourced from the sovereign seg_store (knowledge/store/ecomat) · no TSV · generated epoch " as *u8)
515 o = ss_catn(buf, o, sys_now_realtime_sec()); o = ss_cat(buf, o, " · measured, never asserted" as *u8)
516 if verdict != 0 { o = ss_cat(buf, o, " · <b style=color:#ff7b72>LIAR-KILL FIRED</b>" as *u8) }
517 o = ss_cat(buf, o, "</footer></body></html>\n" as *u8)
518 ss_writefile(outhtml, buf, o)
519 return verdict
520}
521
522// ---- shared target-emit loop (used by the emitter CLI AND the flywheel beat) ----
523// Caller holds the lock and supplies open append fds qf (queue) + lf (targets log). Iterates the store,
524// appends one TODO target per below-bar domain (idempotent vs qb), writes an auto-close marker per at-bar
525// domain. out[0]=emitted out[1]=done_marks. Returns 0, or -1 if the store is missing.
526func et_emit_targets_core(store: *u8, qb: *u8, qn: i64, qf: i64, lf: i64, out: *i64) -> i64 {
527 let h: *i64 = ss_open(store)
528 if (h as i64) == 0 { out[0] = 0; out[1] = 0; return 0 - 1 }
529 let pq: *i64 = sys_mmap(16) as *i64
530 let lq: *i64 = sys_mmap(16) as *i64
531 let idp: *u8 = sys_mmap(160)
532 let dom: *u8 = sys_mmap(ECOMAT_DOM_CAP)
533 let nr: *u8 = sys_mmap(ECOMAT_NR_CAP)
534 let szb: *u8 = sys_mmap(2)
535 var emitted: i64 = 0
536 var done_marks: i64 = 0
537 var k: i64 = 0
538 var go: i64 = 1
539 while go == 1 {
540 let key: *u8 = sys_mmap(64); ec_key(k, key)
541 if ss_hget(h, key, pq, lq) == 1 {
542 let v: *u8 = pq[0] as *u8
543 let cur: i64 = ec_cur(v)
544 let bar: i64 = ec_bar(v)
545 let w: i64 = ec_weight(v)
546 ec_str(v, 0, dom, ECOMAT_DOM_CAP); ec_str(v, 2, nr, ECOMAT_NR_CAP)
547 if cur >= bar {
548 if lf >= 0 { _fp(lf, "ECOMAT-DONE domain=" as *u8); _fp(lf, dom); _fp(lf, " at-bar epoch=" as *u8); _fn(lf, sys_now_realtime_sec()); _fp(lf, "\n" as *u8) }
549 done_marks = done_marks + 1
550 } else {
551 let se: i64 = et_should_emit(dom, cur, bar, qb, qn)
552 if se == 1 {
553 et_mkid(dom, idp)
554 let gap: i64 = bar - cur
555 var sz: i64 = 83
556 if gap == 2 { sz = 77 }
557 if gap >= 3 { sz = 76 }
558 szb[0] = sz as u8; szb[1] = 0 as u8
559 _fp(qf, idp); _fp(qf, "\tMAT\t" as *u8); _fn(qf, w)
560 _fp(qf, "\t" as *u8); _fp(qf, szb)
561 _fp(qf, "\tPM\tTODO\t-\tecomat-" as *u8); _fp(qf, dom)
562 _fp(qf, "-at-bar||MARK=knowledge/status/ecomat_targets.log::ECOMAT-DONE domain=" as *u8); _fp(qf, dom)
563 _fp(qf, "\tmaturity target: raise " as *u8); _fp(qf, dom)
564 _fp(qf, " toward bar -- " as *u8); _fp(qf, nr)
565 _fp(qf, " (autogenerated; w=" as *u8); _fn(qf, w)
566 _fp(qf, "; auto-closes when domain reaches bar)\n" as *u8)
567 if lf >= 0 { _fp(lf, "ECOMAT-TARGET id=" as *u8); _fp(lf, idp); _fp(lf, " w=" as *u8); _fn(lf, w); _fp(lf, " gap=" as *u8); _fn(lf, gap); _fp(lf, "\n" as *u8) }
568 emitted = emitted + 1
569 }
570 }
571 k = k + 1
572 } else { go = 0 }
573 }
574 out[0] = emitted
575 out[1] = done_marks
576 return 0
577}