nx_leaderboard_lib.nx source
↩ module page · 312 lines · 14007 B
1// nx_leaderboard_lib.nx -- THE ONE reader of <dom>.leaderboard (ecosystem EC44, 2026-09-15): leaderboard rows AS DATA,
2// read from mirrored bytes, with the estate's own row placed by arithmetic, never by hand.
3//
4// WHY. The operator's goal names a public leaderboard (BRIGHT) as the evidence surface, and the estate's measuring
5// organ (nx_beir_eval be_bright) already writes the estate's row into knowledge/compare/<dom>.leaderboard -- but no
6// compare page rendered the file, so the number lived in a file nobody published. A LEADERBOARD THE BOARD DOES NOT
7// RENDER IS A CLAIM THE BOARD CANNOT MAKE. This lib is the ruler the emitter's lb_pass projects:
8// head|<board>|<source url>|<mirror>|<pin>|<fetched YYYY-MM-DD>|<metric>
9// lb|<board>|<rank>|<system>|<org>|<score>|<date>|<entry url>
10// A score is decimal with at most one fractional digit (the published boards print nDCG x 100 to one decimal); it is
11// held as score x10 so 66.9 and 14.5 compare as integers. The ESTATE's rows are the ones whose system starts with
12// LB_ESTATE_PREFIX; every other row is a published rival. The estate's RANK is 1 + the number of rival rows whose
13// score is strictly higher (a tie ranks with the tied); its GAP is the leader's score minus its own; its LEAPS are
14// the score deltas between its own rows in date order (the harness appends one row per measurement, dated).
15// license_tier: ORIGINAL. No hw writes (Rule 26).
16import "nx_syscalls.nx"
17
18const LB_DIR: *u8 = "knowledge/compare/"
19const LB_EXT: *u8 = ".leaderboard"
20const LB_PATH_CAP: i64 = 600
21const LB_I64_BYTES: i64 = 8
22const LB_TAG_HEAD: *u8 = "head"
23const LB_TAG_ROW: *u8 = "lb"
24const LB_KIND_OTHER: i64 = 0
25const LB_KIND_HEAD: i64 = 1
26const LB_KIND_ROW: i64 = 2
27const LB_F_TAG: i64 = 0
28const LB_F_BOARD: i64 = 1
29const LB_H_URL: i64 = 2
30const LB_H_MIRROR: i64 = 3
31const LB_H_PIN: i64 = 4
32const LB_H_FETCHED: i64 = 5
33const LB_H_METRIC: i64 = 6
34const LB_H_NF: i64 = 7
35const LB_R_RANK: i64 = 2
36const LB_R_SYSTEM: i64 = 3
37const LB_R_ORG: i64 = 4
38const LB_R_SCORE: i64 = 5
39const LB_R_DATE: i64 = 6
40const LB_R_URL: i64 = 7
41const LB_R_NF: i64 = 8
42const LB_ESTATE_PREFIX: *u8 = "nishi"
43const LB_SCORE_SCALE: i64 = 10 // scores held as x10: one fractional digit
44const LB_CH_PIPE: i64 = 124
45const LB_CH_LF: i64 = 10
46const LB_CH_CR: i64 = 13
47const LB_CH_HASH: i64 = 35
48const LB_CH_DOT: i64 = 46
49const LB_CH_ZERO: i64 = 48
50const LB_CH_NINE: i64 = 57
51const LB_TEN: i64 = 10
52const LB_NONE: i64 = 0 - 1
53
54func lb_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
55func lb_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { d[o + i] = s[i]; i = i + 1 } return o + i }
56func lb_path(dir: *u8, dom: *u8, out: *u8) -> i64 {
57 var o: i64 = lb_cat(out, 0, dir)
58 o = lb_cat(out, o, dom)
59 o = lb_cat(out, o, LB_EXT)
60 out[o] = 0 as u8
61 return o
62}
63// field k of a pipe row [line, line+n): offset and length (a trailing CR is trimmed); 1 when present
64func lb_field(line: *u8, n: i64, k: i64, off: *i64, len: *i64) -> i64 {
65 var f: i64 = 0
66 var s: i64 = 0
67 var i: i64 = 0
68 while i <= n {
69 var hit: i64 = 0
70 if i == n { hit = 1 } else { if line[i] == (LB_CH_PIPE as u8) { hit = 1 } }
71 if hit == 1 {
72 if f == k {
73 var l: i64 = i - s
74 if l > 0 { if line[s + l - 1] == (LB_CH_CR as u8) { l = l - 1 } }
75 off[0] = s; len[0] = l; return 1
76 }
77 f = f + 1
78 s = i + 1
79 }
80 i = i + 1
81 }
82 return 0
83}
84func lb_eq_n(a: *u8, s: *u8, n: i64) -> i64 {
85 if lb_slen(a) != n { return 0 }
86 var i: i64 = 0
87 while i < n { if a[i] != s[i] { return 0 } i = i + 1 }
88 return 1
89}
90func lb_prefix_n(pfx: *u8, s: *u8, n: i64) -> i64 {
91 let pl: i64 = lb_slen(pfx)
92 if n < pl { return 0 }
93 var i: i64 = 0
94 while i < pl { if pfx[i] != s[i] { return 0 } i = i + 1 }
95 return 1
96}
97// field count of a row (pipes + 1)
98func lb_nfields(line: *u8, n: i64) -> i64 {
99 var c: i64 = 1
100 var i: i64 = 0
101 while i < n { if line[i] == (LB_CH_PIPE as u8) { c = c + 1 } i = i + 1 }
102 return c
103}
104// a well-formed head row / lb row of the named board, else OTHER (comments, blanks, other boards, malformed rows)
105func lb_row_kind(line: *u8, n: i64, board: *u8) -> i64 {
106 if n <= 0 { return LB_KIND_OTHER }
107 if line[0] == (LB_CH_HASH as u8) { return LB_KIND_OTHER }
108 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
109 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
110 if lb_field(line, n, LB_F_TAG, off, len) == 0 { return LB_KIND_OTHER }
111 var kind: i64 = LB_KIND_OTHER
112 if lb_eq_n(LB_TAG_HEAD, (line as i64 + off[0]) as *u8, len[0]) == 1 { kind = LB_KIND_HEAD }
113 if lb_eq_n(LB_TAG_ROW, (line as i64 + off[0]) as *u8, len[0]) == 1 { kind = LB_KIND_ROW }
114 if kind == LB_KIND_OTHER { return LB_KIND_OTHER }
115 if lb_field(line, n, LB_F_BOARD, off, len) == 0 { return LB_KIND_OTHER }
116 if lb_eq_n(board, (line as i64 + off[0]) as *u8, len[0]) == 0 { return LB_KIND_OTHER }
117 let nf: i64 = lb_nfields(line, n)
118 if kind == LB_KIND_HEAD { if nf != LB_H_NF { return LB_KIND_OTHER } }
119 if kind == LB_KIND_ROW { if nf != LB_R_NF { return LB_KIND_OTHER } }
120 // a row whose score does not parse cannot vote on any rank: it is OTHER, and the pass counts it as malformed
121 if kind == LB_KIND_ROW { if lb_row_score(line, n) == LB_NONE { return LB_KIND_OTHER } }
122 return kind
123}
124// "66.9" -> 669, "14.5" -> 145, "7" -> 70, "40" -> 400; a second fractional digit is dropped (announced by the scale);
125// anything that is not a decimal reads LB_NONE
126func lb_score_x10(s: *u8, n: i64) -> i64 {
127 var v: i64 = 0
128 var i: i64 = 0
129 var digits: i64 = 0
130 while i < n {
131 let c: i64 = s[i] as i64
132 if c == LB_CH_DOT { break }
133 if c < LB_CH_ZERO { return LB_NONE }
134 if c > LB_CH_NINE { return LB_NONE }
135 v = v * LB_TEN + (c - LB_CH_ZERO)
136 digits = digits + 1
137 i = i + 1
138 }
139 if digits == 0 { return LB_NONE }
140 v = v * LB_SCORE_SCALE
141 if i < n { if s[i] == (LB_CH_DOT as u8) {
142 if i + 1 < n {
143 let c2: i64 = s[i + 1] as i64
144 if c2 < LB_CH_ZERO { return LB_NONE }
145 if c2 > LB_CH_NINE { return LB_NONE }
146 v = v + (c2 - LB_CH_ZERO)
147 }
148 } }
149 return v
150}
151func lb_row_score(line: *u8, n: i64) -> i64 {
152 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
153 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
154 if lb_field(line, n, LB_R_SCORE, off, len) == 0 { return LB_NONE }
155 return lb_score_x10((line as i64 + off[0]) as *u8, len[0])
156}
157func lb_row_is_estate(line: *u8, n: i64) -> i64 {
158 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
159 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
160 if lb_field(line, n, LB_R_SYSTEM, off, len) == 0 { return 0 }
161 return lb_prefix_n(LB_ESTATE_PREFIX, (line as i64 + off[0]) as *u8, len[0])
162}
163// walk the file's lines: i-th line offset/len; returns the line count (a walker the passes below share)
164func lb_line(buf: *u8, n: i64, from: i64, off: *i64, len: *i64) -> i64 {
165 if from >= n { return 0 }
166 var e: i64 = from
167 while e < n { if buf[e] == (LB_CH_LF as u8) { break } e = e + 1 }
168 off[0] = from
169 len[0] = e - from
170 return e + 1
171}
172// counts for one board: heads, rival rows, estate rows, malformed rows tagged head|lb for that board name are counted
173// as OTHER by lb_row_kind (they do not vote); returns rows (rival + estate)
174func lb_count(buf: *u8, n: i64, board: *u8, out_heads: *i64, out_rivals: *i64, out_estate: *i64) -> i64 {
175 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
176 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
177 var heads: i64 = 0
178 var rivals: i64 = 0
179 var estate: i64 = 0
180 var p: i64 = 0
181 while p < n {
182 let next: i64 = lb_line(buf, n, p, off, len)
183 let line: *u8 = (buf as i64 + off[0]) as *u8
184 let k: i64 = lb_row_kind(line, len[0], board)
185 if k == LB_KIND_HEAD { heads = heads + 1 }
186 if k == LB_KIND_ROW { if lb_row_is_estate(line, len[0]) == 1 { estate = estate + 1 } else { rivals = rivals + 1 } }
187 p = next
188 }
189 out_heads[0] = heads
190 out_rivals[0] = rivals
191 out_estate[0] = estate
192 return rivals + estate
193}
194// the leader among the RIVAL rows (score x10), LB_NONE when there are none
195func lb_leader(buf: *u8, n: i64, board: *u8) -> i64 {
196 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
197 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
198 var best: i64 = LB_NONE
199 var p: i64 = 0
200 while p < n {
201 let next: i64 = lb_line(buf, n, p, off, len)
202 let line: *u8 = (buf as i64 + off[0]) as *u8
203 if lb_row_kind(line, len[0], board) == LB_KIND_ROW { if lb_row_is_estate(line, len[0]) == 0 {
204 let sc: i64 = lb_row_score(line, len[0])
205 if sc > best { best = sc }
206 } }
207 p = next
208 }
209 return best
210}
211// the rank a score x10 earns among the RIVAL rows: 1 + rows strictly above it (a tie ranks with the tied)
212func lb_rank_of(buf: *u8, n: i64, board: *u8, score_x10: i64) -> i64 {
213 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
214 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
215 var above: i64 = 0
216 var p: i64 = 0
217 while p < n {
218 let next: i64 = lb_line(buf, n, p, off, len)
219 let line: *u8 = (buf as i64 + off[0]) as *u8
220 if lb_row_kind(line, len[0], board) == LB_KIND_ROW { if lb_row_is_estate(line, len[0]) == 0 {
221 let sc: i64 = lb_row_score(line, len[0])
222 if sc > score_x10 { above = above + 1 }
223 } }
224 p = next
225 }
226 return above + 1
227}
228// the estate's LATEST row (by date field, lexical YYYY-MM-DD; a later file row wins a tie): its score x10, or
229// LB_NONE when the estate has no row; the row's offset/len are handed back for rendering
230func lb_estate_latest(buf: *u8, n: i64, board: *u8, roff: *i64, rlen: *i64) -> i64 {
231 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
232 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
233 let doff: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
234 let dlen: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
235 var best: i64 = LB_NONE
236 var bdoff: i64 = 0
237 var bdlen: i64 = 0
238 var p: i64 = 0
239 while p < n {
240 let next: i64 = lb_line(buf, n, p, off, len)
241 let line: *u8 = (buf as i64 + off[0]) as *u8
242 if lb_row_kind(line, len[0], board) == LB_KIND_ROW { if lb_row_is_estate(line, len[0]) == 1 {
243 if lb_field(line, len[0], LB_R_DATE, doff, dlen) == 1 {
244 var later: i64 = 1
245 if best != LB_NONE { later = lb_date_ge((line as i64 + doff[0]) as *u8, dlen[0], (buf as i64 + bdoff) as *u8, bdlen) }
246 if later == 1 { best = lb_row_score(line, len[0]); bdoff = off[0] + doff[0]; bdlen = dlen[0]; roff[0] = off[0]; rlen[0] = len[0] }
247 }
248 } }
249 p = next
250 }
251 return best
252}
253// lexical compare of two date runs: 1 when a >= b (YYYY-MM-DD sorts lexically)
254func lb_date_ge(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
255 var i: i64 = 0
256 while i < an { if i >= bn { return 1 } if a[i] != b[i] { if (a[i] as i64) > (b[i] as i64) { return 1 } return 0 } i = i + 1 }
257 if an >= bn { return 1 }
258 return 0
259}
260// the estate's LEAP: latest score minus the score of its earliest row (LB_NONE when fewer than two estate rows)
261func lb_estate_leap(buf: *u8, n: i64, board: *u8) -> i64 {
262 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
263 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
264 let doff: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
265 let dlen: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
266 var first: i64 = LB_NONE
267 var fdoff: i64 = 0
268 var fdlen: i64 = 0
269 var count: i64 = 0
270 var p: i64 = 0
271 while p < n {
272 let next: i64 = lb_line(buf, n, p, off, len)
273 let line: *u8 = (buf as i64 + off[0]) as *u8
274 if lb_row_kind(line, len[0], board) == LB_KIND_ROW { if lb_row_is_estate(line, len[0]) == 1 {
275 if lb_field(line, len[0], LB_R_DATE, doff, dlen) == 1 {
276 count = count + 1
277 var earlier: i64 = 1
278 if first != LB_NONE { earlier = 1 - lb_date_ge((line as i64 + doff[0]) as *u8, dlen[0], (buf as i64 + fdoff) as *u8, fdlen) }
279 if earlier == 1 { first = lb_row_score(line, len[0]); fdoff = off[0] + doff[0]; fdlen = dlen[0] }
280 }
281 } }
282 p = next
283 }
284 if count < 2 { return LB_NONE }
285 let roff: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
286 let rlen: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
287 let latest: i64 = lb_estate_latest(buf, n, board, roff, rlen)
288 return latest - first
289}
290// the distinct boards named by head rows, in file order: offsets/lengths of the board names; returns the count
291func lb_boards(buf: *u8, n: i64, boff: *i64, blen: *i64, cap: i64) -> i64 {
292 let off: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
293 let len: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
294 let f: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
295 let fl: *i64 = sys_mmap(LB_I64_BYTES * 2) as *i64
296 var nb: i64 = 0
297 var p: i64 = 0
298 while p < n {
299 let next: i64 = lb_line(buf, n, p, off, len)
300 let line: *u8 = (buf as i64 + off[0]) as *u8
301 var ishead: i64 = 0
302 if len[0] > 0 { if line[0] != (LB_CH_HASH as u8) { if lb_field(line, len[0], LB_F_TAG, f, fl) == 1 { if lb_eq_n(LB_TAG_HEAD, (line as i64 + f[0]) as *u8, fl[0]) == 1 { ishead = 1 } } } }
303 if ishead == 1 { if lb_field(line, len[0], LB_F_BOARD, f, fl) == 1 {
304 var seen: i64 = 0
305 var k: i64 = 0
306 while k < nb { if blen[k] == fl[0] { var same: i64 = 1; var j: i64 = 0; while j < fl[0] { if buf[boff[k] + j] != line[f[0] + j] { same = 0; j = fl[0] } else { j = j + 1 } } if same == 1 { seen = 1 } } k = k + 1 }
307 if seen == 0 { if nb < cap { boff[nb] = off[0] + f[0]; blen[nb] = fl[0]; nb = nb + 1 } }
308 } }
309 p = next
310 }
311 return nb
312}