nx_assets_lib.nx source
↩ module page · 393 lines · 17804 B
1// nx_assets_lib.nx -- THE ONE READER of <dom>.assets (2026-09-16): every FOREIGN ASSET a board's run path or
2// candidate arms depend on, as data, with its licence, its bytes, where it is used, the rung that replaces it
3// with ours, and the three-point lift to get there.
4//
5// WHY. Operator 2026-09-16, after choosing "open weights now, ours over time": "yes if we need 3rd party today
6// then id like to know via the /compare capabilities what the lift would be to replace a 3rd party we are using
7// thats open source etc with our own in cost" -- "the goal always being a reduction in 3rd parties from the first
8// byte up but a focus on capability". The sovereignty ledger (nx_selfsuff) measures outside HOSTS and CARRIERS
9// from what the code dials; it has no row for a DATA asset the code reads -- a model's weights, a font, a
10// benchmark corpus, a vendor driver behind a door -- and those are exactly the foreign bytes the standing order
11// admits ("DATA assets are the only foreign bytes allowed inside, and each carries its license beside it").
12// This lib gives them a row, a state, and a price.
13//
14// ROW (14 fields, no pipes in prose):
15// asset|<key>|<state>|<kind>|<name>|<licence>|<bytes>|<used_by>|<rung>|<p10>|<p50>|<p90>|<basis>|<note>
16// state: runpath (read by an organ on the serving or scoring lane today) | candidate (a licensed asset the
17// board intends to run on the sovereign engines, not yet in the run path) | oracle (a benchmark or
18// reference outside the build and run path, never a dependency) | unavailable (named by the field but
19// not obtainable: a licence tag over an empty repository is the honest example)
20// kind: weights | font | dataset | door | spec | card
21// bytes: the measured size, or - when not measured (counted as UNKNOWN, never as zero)
22// rung: the plan rung that replaces the asset with ours; <dom>:<rung> for a rung on another board (XBOARD, not
23// verifiable here); - is a DEFECT on a runpath or candidate row (NORUNG: a foreign asset with no path to
24// replacing it is what this ledger exists to expose) and correct on an oracle row
25// p10 p50 p90: the lift in u (the board's own unit, one fractional digit) to land the replacement rung; - on
26// an oracle or unavailable row; on a runpath or candidate row a missing lift is LIFT-MISSING
27// basis: the components the estimate was built from (named, so a reader can dispute one)
28// One verdict per row, the first failing rule wins: OK FIELDS BADSTATE BADKIND BADLIFT LIFT-MISSING NORUNG
29// BADRUNG. Totals partition by state and by verdict and both partitions SUM to rows; the run-path lift is
30// aggregated through nx_costest_lib ces_agg3 (root-sum-square band beside the correlated sum).
31// license_tier: ORIGINAL No hw writes (Rule 26).
32import "nx_syscalls.nx"
33import "nx_costest_lib.nx"
34
35const FAS_I64: i64 = 8
36const FAS_STRIDE: i64 = 32
37const FAS_KEY_O: i64 = 0
38const FAS_KEY_L: i64 = 1
39const FAS_STATE_O: i64 = 2
40const FAS_STATE_L: i64 = 3
41const FAS_KIND_O: i64 = 4
42const FAS_KIND_L: i64 = 5
43const FAS_NAME_O: i64 = 6
44const FAS_NAME_L: i64 = 7
45const FAS_LIC_O: i64 = 8
46const FAS_LIC_L: i64 = 9
47const FAS_USED_O: i64 = 10
48const FAS_USED_L: i64 = 11
49const FAS_RUNG_O: i64 = 12
50const FAS_RUNG_L: i64 = 13
51const FAS_BASIS_O: i64 = 14
52const FAS_BASIS_L: i64 = 15
53const FAS_NOTE_O: i64 = 16
54const FAS_NOTE_L: i64 = 17
55const FAS_BYTES: i64 = 18
56const FAS_P10: i64 = 19
57const FAS_P50: i64 = 20
58const FAS_P90: i64 = 21
59const FAS_STATE: i64 = 22
60const FAS_KIND: i64 = 23
61const FAS_VERDICT: i64 = 24
62const FAS_RUNGST: i64 = 25
63const FAS_NF: i64 = 26
64const FAS_LINE: i64 = 27
65// row grammar
66const FAS_TAG: *u8 = "asset"
67const FAS_F_KEY: i64 = 1
68const FAS_F_STATE: i64 = 2
69const FAS_F_KIND: i64 = 3
70const FAS_F_NAME: i64 = 4
71const FAS_F_LIC: i64 = 5
72const FAS_F_BYTES: i64 = 6
73const FAS_F_USED: i64 = 7
74const FAS_F_RUNG: i64 = 8
75const FAS_F_P10: i64 = 9
76const FAS_F_P50: i64 = 10
77const FAS_F_P90: i64 = 11
78const FAS_F_BASIS: i64 = 12
79const FAS_F_NOTE: i64 = 13
80const FAS_NF_ROW: i64 = 14
81const FAS_MIN_ROW: i64 = 19 // "asset" plus thirteen pipes plus a newline: the shortest row, so bytes/19+1 bounds the count
82// states
83const FAS_ST_BAD: i64 = 0
84const FAS_ST_RUNPATH: i64 = 1
85const FAS_ST_CANDIDATE: i64 = 2
86const FAS_ST_ORACLE: i64 = 3
87const FAS_ST_UNAVAILABLE: i64 = 4
88const FAS_ST_N: i64 = 5
89// kinds
90const FAS_K_BAD: i64 = 0
91const FAS_K_WEIGHTS: i64 = 1
92const FAS_K_FONT: i64 = 2
93const FAS_K_DATASET: i64 = 3
94const FAS_K_DOOR: i64 = 4
95const FAS_K_SPEC: i64 = 5
96const FAS_K_CARD: i64 = 6
97const FAS_K_N: i64 = 7
98// verdicts
99const FAS_V_OK: i64 = 0
100const FAS_V_FIELDS: i64 = 1
101const FAS_V_BADSTATE: i64 = 2
102const FAS_V_BADKIND: i64 = 3
103const FAS_V_BADLIFT: i64 = 4
104const FAS_V_LIFTMISSING: i64 = 5
105const FAS_V_NORUNG: i64 = 6
106const FAS_V_BADRUNG: i64 = 7
107const FAS_V_N: i64 = 8
108// rung states
109const FAS_R_NONE: i64 = 0
110const FAS_R_LOCAL: i64 = 1
111const FAS_R_MISSING: i64 = 2
112const FAS_R_XBOARD: i64 = 3
113// totals slots
114const FAS_T_ROWS: i64 = 0
115const FAS_T_ST0: i64 = 1 // + state code
116const FAS_T_V0: i64 = 6 // + verdict code
117const FAS_T_RP_BYTES: i64 = 14
118const FAS_T_RP_UNKNOWN: i64 = 15
119const FAS_T_RP_AGG: i64 = 16 // + CES_A_* (8 slots)
120const FAS_T_CA_AGG: i64 = 24 // + CES_A_* (8 slots)
121const FAS_T_DEFECTS: i64 = 32
122const FAS_T_XBOARD: i64 = 33
123const FAS_T_RP_PRICED: i64 = 34
124const FAS_T_CA_PRICED: i64 = 35
125const FAS_T_SLOTS: i64 = 40
126const FAS_CH_DASH: i64 = 45
127const FAS_CH_COLON: i64 = 58
128const FAS_CH_HASH: i64 = 35
129const FAS_CH_PIPE: i64 = 124
130const FAS_RUNG_TAG: *u8 = "rung"
131const FAS_RUNG_F_ID: i64 = 1
132
133func fas_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
134func fas_cap(bytes: i64) -> i64 { return bytes / FAS_MIN_ROW + 1 }
135
136func fas_state_code(buf: *u8, off: i64, len: i64) -> i64 {
137 if ces_span_is(buf, off, len, "runpath" as *u8) == 1 { return FAS_ST_RUNPATH }
138 if ces_span_is(buf, off, len, "candidate" as *u8) == 1 { return FAS_ST_CANDIDATE }
139 if ces_span_is(buf, off, len, "oracle" as *u8) == 1 { return FAS_ST_ORACLE }
140 if ces_span_is(buf, off, len, "unavailable" as *u8) == 1 { return FAS_ST_UNAVAILABLE }
141 return FAS_ST_BAD
142}
143func fas_kind_code(buf: *u8, off: i64, len: i64) -> i64 {
144 if ces_span_is(buf, off, len, "weights" as *u8) == 1 { return FAS_K_WEIGHTS }
145 if ces_span_is(buf, off, len, "font" as *u8) == 1 { return FAS_K_FONT }
146 if ces_span_is(buf, off, len, "dataset" as *u8) == 1 { return FAS_K_DATASET }
147 if ces_span_is(buf, off, len, "door" as *u8) == 1 { return FAS_K_DOOR }
148 if ces_span_is(buf, off, len, "spec" as *u8) == 1 { return FAS_K_SPEC }
149 if ces_span_is(buf, off, len, "card" as *u8) == 1 { return FAS_K_CARD }
150 return FAS_K_BAD
151}
152func fas_state_name(c: i64) -> *u8 {
153 if c == FAS_ST_RUNPATH { return "runpath" as *u8 }
154 if c == FAS_ST_CANDIDATE { return "candidate" as *u8 }
155 if c == FAS_ST_ORACLE { return "oracle" as *u8 }
156 if c == FAS_ST_UNAVAILABLE { return "unavailable" as *u8 }
157 return "BAD-STATE" as *u8
158}
159func fas_kind_name(c: i64) -> *u8 {
160 if c == FAS_K_WEIGHTS { return "weights" as *u8 }
161 if c == FAS_K_FONT { return "font" as *u8 }
162 if c == FAS_K_DATASET { return "dataset" as *u8 }
163 if c == FAS_K_DOOR { return "door" as *u8 }
164 if c == FAS_K_SPEC { return "spec" as *u8 }
165 if c == FAS_K_CARD { return "card" as *u8 }
166 return "BAD-KIND" as *u8
167}
168func fas_verdict_name(v: i64) -> *u8 {
169 if v == FAS_V_OK { return "OK" as *u8 }
170 if v == FAS_V_FIELDS { return "FIELDS" as *u8 }
171 if v == FAS_V_BADSTATE { return "BADSTATE" as *u8 }
172 if v == FAS_V_BADKIND { return "BADKIND" as *u8 }
173 if v == FAS_V_BADLIFT { return "BADLIFT" as *u8 }
174 if v == FAS_V_LIFTMISSING { return "LIFT-MISSING" as *u8 }
175 if v == FAS_V_NORUNG { return "NORUNG" as *u8 }
176 if v == FAS_V_BADRUNG { return "BADRUNG" as *u8 }
177 return "UNKNOWN" as *u8
178}
179func fas_rungstate_name(r: i64) -> *u8 {
180 if r == FAS_R_NONE { return "none" as *u8 }
181 if r == FAS_R_LOCAL { return "local" as *u8 }
182 if r == FAS_R_MISSING { return "missing" as *u8 }
183 if r == FAS_R_XBOARD { return "xboard" as *u8 }
184 return "unknown" as *u8
185}
186
187// the span holds a colon: a <dom>:<rung> cross-board reference
188func fas_span_has_colon(buf: *u8, off: i64, len: i64) -> i64 {
189 var i: i64 = 0
190 while i < len { if buf[off + i] == (FAS_CH_COLON as u8) { return 1 } i = i + 1 }
191 return 0
192}
193
194// one row's text field into the table
195func fas_take(buf: *u8, p: i64, e: i64, k: i64, tab: *i64, b: i64, oslot: i64, lslot: i64, off: *i64) -> i64 {
196 let l: i64 = ces_field(buf, p, e, k, off)
197 if l < 0 { tab[b + oslot] = p; tab[b + lslot] = 0; return 0 }
198 tab[b + oslot] = off[0]; tab[b + lslot] = l
199 return l
200}
201
202// parse every asset row of buf into tab (stride FAS_STRIDE, capacity cap rows); intra-row judgement applied.
203// Returns the row count. The rung existence check needs the plan and is fas_judge_rungs.
204func fas_load(buf: *u8, n: i64, tab: *i64, cap: i64) -> i64 {
205 let off: *i64 = sys_mmap(FAS_I64) as *i64
206 var rows: i64 = 0
207 var line: i64 = 0
208 var p: i64 = 0
209 while p < n {
210 let e: i64 = ces_line_end(buf, n, p)
211 line = line + 1
212 let l0: i64 = ces_field(buf, p, e, 0, off)
213 if ces_span_is(buf, off[0], l0, FAS_TAG) == 1 { if rows < cap {
214 let b: i64 = rows * FAS_STRIDE
215 var s: i64 = 0
216 while s < FAS_STRIDE { tab[b + s] = 0; s = s + 1 }
217 tab[b + FAS_LINE] = line
218 let nf: i64 = ces_nfields(buf, p, e)
219 tab[b + FAS_NF] = nf
220 fas_take(buf, p, e, FAS_F_KEY, tab, b, FAS_KEY_O, FAS_KEY_L, off)
221 fas_take(buf, p, e, FAS_F_STATE, tab, b, FAS_STATE_O, FAS_STATE_L, off)
222 fas_take(buf, p, e, FAS_F_KIND, tab, b, FAS_KIND_O, FAS_KIND_L, off)
223 fas_take(buf, p, e, FAS_F_NAME, tab, b, FAS_NAME_O, FAS_NAME_L, off)
224 fas_take(buf, p, e, FAS_F_LIC, tab, b, FAS_LIC_O, FAS_LIC_L, off)
225 fas_take(buf, p, e, FAS_F_USED, tab, b, FAS_USED_O, FAS_USED_L, off)
226 fas_take(buf, p, e, FAS_F_RUNG, tab, b, FAS_RUNG_O, FAS_RUNG_L, off)
227 fas_take(buf, p, e, FAS_F_BASIS, tab, b, FAS_BASIS_O, FAS_BASIS_L, off)
228 fas_take(buf, p, e, FAS_F_NOTE, tab, b, FAS_NOTE_O, FAS_NOTE_L, off)
229 let bl: i64 = ces_field(buf, p, e, FAS_F_BYTES, off)
230 var bytes: i64 = CES_NONE
231 if bl == 1 { if buf[off[0]] == (FAS_CH_DASH as u8) { bytes = CES_NONE } else { bytes = ces_parse_int(buf, off[0], bl) } } else { bytes = ces_parse_int(buf, off[0], bl) }
232 tab[b + FAS_BYTES] = bytes
233 let l10: i64 = ces_field(buf, p, e, FAS_F_P10, off); tab[b + FAS_P10] = ces_parse_deciu(buf, off[0], l10)
234 let l50: i64 = ces_field(buf, p, e, FAS_F_P50, off); tab[b + FAS_P50] = ces_parse_deciu(buf, off[0], l50)
235 let l90: i64 = ces_field(buf, p, e, FAS_F_P90, off); tab[b + FAS_P90] = ces_parse_deciu(buf, off[0], l90)
236 let st: i64 = fas_state_code(buf, tab[b + FAS_STATE_O], tab[b + FAS_STATE_L])
237 let kd: i64 = fas_kind_code(buf, tab[b + FAS_KIND_O], tab[b + FAS_KIND_L])
238 tab[b + FAS_STATE] = st
239 tab[b + FAS_KIND] = kd
240 // rung state (existence is judged against the plan later)
241 var rs: i64 = FAS_R_LOCAL
242 let ro: i64 = tab[b + FAS_RUNG_O]
243 let rl: i64 = tab[b + FAS_RUNG_L]
244 if rl <= 0 { rs = FAS_R_NONE } else {
245 if rl == 1 { if buf[ro] == (FAS_CH_DASH as u8) { rs = FAS_R_NONE } }
246 if rs != FAS_R_NONE { if fas_span_has_colon(buf, ro, rl) == 1 { rs = FAS_R_XBOARD } }
247 }
248 tab[b + FAS_RUNGST] = rs
249 // the verdict: first failing rule wins
250 var v: i64 = FAS_V_OK
251 if nf != FAS_NF_ROW { v = FAS_V_FIELDS }
252 if v == FAS_V_OK { if st == FAS_ST_BAD { v = FAS_V_BADSTATE } }
253 if v == FAS_V_OK { if kd == FAS_K_BAD { v = FAS_V_BADKIND } }
254 if v == FAS_V_OK {
255 let a: i64 = tab[b + FAS_P10]
256 let m: i64 = tab[b + FAS_P50]
257 let z: i64 = tab[b + FAS_P90]
258 var malformed: i64 = 0
259 if a == CES_NONE { malformed = 1 }
260 if m == CES_NONE { malformed = 1 }
261 if z == CES_NONE { malformed = 1 }
262 var absent: i64 = 0
263 if a == CES_ABSENT { absent = 1 }
264 if m == CES_ABSENT { absent = 1 }
265 if z == CES_ABSENT { absent = 1 }
266 if malformed == 1 { v = FAS_V_BADLIFT } else {
267 var priced: i64 = 0
268 if st == FAS_ST_RUNPATH { priced = 1 }
269 if st == FAS_ST_CANDIDATE { priced = 1 }
270 if priced == 1 {
271 if absent == 1 { v = FAS_V_LIFTMISSING } else {
272 if a > m { v = FAS_V_BADLIFT }
273 if m > z { v = FAS_V_BADLIFT }
274 }
275 } else {
276 if absent == 0 { if a > m { v = FAS_V_BADLIFT } }
277 if absent == 0 { if m > z { v = FAS_V_BADLIFT } }
278 }
279 }
280 }
281 if v == FAS_V_OK {
282 var needs_rung: i64 = 0
283 if st == FAS_ST_RUNPATH { needs_rung = 1 }
284 if st == FAS_ST_CANDIDATE { needs_rung = 1 }
285 if needs_rung == 1 { if rs == FAS_R_NONE { v = FAS_V_NORUNG } }
286 }
287 tab[b + FAS_VERDICT] = v
288 rows = rows + 1
289 } }
290 p = e + 1
291 }
292 return rows
293}
294
295// does the plan declare rung <id> (a line whose field 0 is "rung" and field 1 is the id)?
296func fas_plan_has_rung(plan: *u8, pn: i64, buf: *u8, roff: i64, rlen: i64) -> i64 {
297 let off: *i64 = sys_mmap(FAS_I64) as *i64
298 var p: i64 = 0
299 while p < pn {
300 let e: i64 = ces_line_end(plan, pn, p)
301 let l0: i64 = ces_field(plan, p, e, 0, off)
302 if ces_span_is(plan, off[0], l0, FAS_RUNG_TAG) == 1 {
303 let il: i64 = ces_field(plan, p, e, FAS_RUNG_F_ID, off)
304 if il == rlen {
305 var same: i64 = 1
306 var i: i64 = 0
307 while i < il { if plan[off[0] + i] != buf[roff + i] { same = 0 } i = i + 1 }
308 if same == 1 { return 1 }
309 }
310 }
311 p = e + 1
312 }
313 return 0
314}
315
316// judge every LOCAL rung against the plan; an OK row whose rung is absent becomes BADRUNG. Returns the missing count.
317func fas_judge_rungs(tab: *i64, rows: i64, buf: *u8, plan: *u8, pn: i64) -> i64 {
318 var missing: i64 = 0
319 var r: i64 = 0
320 while r < rows {
321 let b: i64 = r * FAS_STRIDE
322 if tab[b + FAS_RUNGST] == FAS_R_LOCAL {
323 if fas_plan_has_rung(plan, pn, buf, tab[b + FAS_RUNG_O], tab[b + FAS_RUNG_L]) == 0 {
324 tab[b + FAS_RUNGST] = FAS_R_MISSING
325 missing = missing + 1
326 if tab[b + FAS_VERDICT] == FAS_V_OK { tab[b + FAS_VERDICT] = FAS_V_BADRUNG }
327 }
328 }
329 r = r + 1
330 }
331 return missing
332}
333
334// the totals: partitions by state and by verdict (both sum to rows), run-path bytes, and the lift aggregates
335func fas_totals(tab: *i64, rows: i64, out: *i64) -> i64 {
336 var s: i64 = 0
337 while s < FAS_T_SLOTS { out[s] = 0; s = s + 1 }
338 out[FAS_T_ROWS] = rows
339 let rp10: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
340 let rp50: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
341 let rp90: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
342 let cp10: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
343 let cp50: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
344 let cp90: *i64 = sys_mmap((rows + 1) * FAS_I64) as *i64
345 var nrp: i64 = 0
346 var ncp: i64 = 0
347 var r: i64 = 0
348 while r < rows {
349 let b: i64 = r * FAS_STRIDE
350 let st: i64 = tab[b + FAS_STATE]
351 let v: i64 = tab[b + FAS_VERDICT]
352 out[FAS_T_ST0 + st] = out[FAS_T_ST0 + st] + 1
353 out[FAS_T_V0 + v] = out[FAS_T_V0 + v] + 1
354 if v != FAS_V_OK { out[FAS_T_DEFECTS] = out[FAS_T_DEFECTS] + 1 }
355 if tab[b + FAS_RUNGST] == FAS_R_XBOARD { out[FAS_T_XBOARD] = out[FAS_T_XBOARD] + 1 }
356 if st == FAS_ST_RUNPATH {
357 if tab[b + FAS_BYTES] >= 0 { out[FAS_T_RP_BYTES] = out[FAS_T_RP_BYTES] + tab[b + FAS_BYTES] } else { out[FAS_T_RP_UNKNOWN] = out[FAS_T_RP_UNKNOWN] + 1 }
358 }
359 var priced: i64 = 1
360 if tab[b + FAS_P10] < 0 { priced = 0 }
361 if tab[b + FAS_P50] < 0 { priced = 0 }
362 if tab[b + FAS_P90] < 0 { priced = 0 }
363 if v == FAS_V_BADLIFT { priced = 0 }
364 if priced == 1 {
365 if st == FAS_ST_RUNPATH { rp10[nrp] = tab[b + FAS_P10]; rp50[nrp] = tab[b + FAS_P50]; rp90[nrp] = tab[b + FAS_P90]; nrp = nrp + 1 }
366 if st == FAS_ST_CANDIDATE { cp10[ncp] = tab[b + FAS_P10]; cp50[ncp] = tab[b + FAS_P50]; cp90[ncp] = tab[b + FAS_P90]; ncp = ncp + 1 }
367 }
368 r = r + 1
369 }
370 let agg: *i64 = sys_mmap(CES_A_SLOTS * FAS_I64) as *i64
371 ces_agg3(rp10, rp50, rp90, nrp, agg)
372 var k: i64 = 0
373 while k < CES_A_SLOTS { out[FAS_T_RP_AGG + k] = agg[k]; k = k + 1 }
374 ces_agg3(cp10, cp50, cp90, ncp, agg)
375 k = 0
376 while k < CES_A_SLOTS { out[FAS_T_CA_AGG + k] = agg[k]; k = k + 1 }
377 out[FAS_T_RP_PRICED] = nrp
378 out[FAS_T_CA_PRICED] = ncp
379 return rows
380}
381
382func fas_state_sum(out: *i64) -> i64 {
383 var t: i64 = 0
384 var i: i64 = 0
385 while i < FAS_ST_N { t = t + out[FAS_T_ST0 + i]; i = i + 1 }
386 return t
387}
388func fas_verdict_sum(out: *i64) -> i64 {
389 var t: i64 = 0
390 var i: i64 = 0
391 while i < FAS_V_N { t = t + out[FAS_T_V0 + i]; i = i + 1 }
392 return t
393}