code wiki / (root) / nx_lbclimb.nx

nx_lbclimb.nx source

↩ module page · 285 lines · 13944 B

1// nx_lbclimb.nx -- CLI over nx_lbclimb_lib (search R0d, ecosystem EC62, 2026-09-17): the estate answers "what ladder 2// do we climb to beat the leader" from DATA -- the domain's plan (dated targets, contender rungs, climbboard and 3// climbgate rows) joined with its leaderboard file (rival rows, the estate's own measured row). 4// nx_lbclimb <domain> [board] [journal=<path>] 5// journal=<path> appends ONE row per judged board (lbclimb|epoch|domain|board|target|leader=..|...|VERDICT), built in a 6// buffer and written with one write, so a daily beat leaves a time spine of where the estate stands against the leader. 7// Per board: the leader, the estate's score and rank, then every contender toward the board's target in floor order 8// (the rank its gate reaches, whether that moves the estate's rank, whether it clears the leader), the partition 9// contenders = gated + ungated + nogate + badfloor, strays and duplicate gates, and the verdict LAST. 10// exit 0 REACHES | 1 SHORT or BADTARGET | 2 usage | 3 abstain (NOGATES, NOBOARD, no climbboard row, no plan) 11// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 2 12import "nx_syscalls.nx" 13import "nx_barfresh_lib.nx" 14import "nx_ladder_lib.nx" 15import "nx_leaderboard_lib.nx" 16import "nx_lbclimb_lib.nx" 17 18const LCC_ARG_DOMAIN: i64 = 1 19const LCC_ARG_BOARD: i64 = 2 20const LCC_EXIT_USAGE: i64 = 2 21const LCC_STDOUT: i64 = 1 22const LCC_NUM_CAP: i64 = 24 23const LCC_BOARD_CAP: i64 = 16 24const LCC_TEN: i64 = 10 25const LCC_LB_SUFFIX: *u8 = ".leaderboard" 26const LCC_TITLE_MAX: i64 = 96 27const LCC_JRNL_PFX: *u8 = "journal=" 28const LCC_JRNL_PFX_LEN: i64 = 8 29const LCC_ROW_CAP: i64 = 768 30const LCC_MODE_644: i64 = 420 31const LCC_CLOCK_WORDS: i64 = 2 32const LCC_NOFD: i64 = 0 - 1 33 34func lcc_w(s: *u8) -> i64 { return sys_write(LCC_STDOUT, s, bf_slen(s)) } 35func lcc_wn(v: i64) -> i64 { 36 let b: *u8 = sys_mmap(LCC_NUM_CAP) 37 var x: i64 = v 38 var neg: i64 = 0 39 if x < 0 { neg = 1; x = 0 - x } 40 var o: i64 = LCC_NUM_CAP - 1 41 b[o] = 0 as u8 42 o = o - 1 43 if x == 0 { b[o] = BF_DIGIT0 as u8; o = o - 1 } 44 while x > 0 { b[o] = (x - (x / LCC_TEN) * LCC_TEN + BF_DIGIT0) as u8; o = o - 1; x = x / LCC_TEN } 45 if neg == 1 { b[o] = BF_DASH as u8; o = o - 1 } 46 return lcc_w((b as i64 + o + 1) as *u8) 47} 48func lcc_wspan(buf: *u8, off: i64, len: i64) -> i64 { 49 if len <= 0 { return 0 } 50 return sys_write(LCC_STDOUT, (buf as i64 + off) as *u8, len) 51} 52// a score held x10 printed the way the public boards print it; the dash when there is none 53func lcc_wscore(v: i64) -> i64 { 54 if v == LBC_NONE { lcc_w("-" as *u8); return 0 } 55 lcc_wn(v / LCC_TEN); lcc_w("." as *u8); lcc_wn(v - (v / LCC_TEN) * LCC_TEN) 56 return 0 57} 58 59// the journal row is built in one buffer: cat a C string, a span, a number, a score 60func lcc_bcat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = o; while s[i] != (0 as u8) { if p < LCC_ROW_CAP - 2 { d[p] = s[i]; p = p + 1 } i = i + 1 } return p } 61func lcc_bspan(d: *u8, o: i64, buf: *u8, off: i64, len: i64) -> i64 { var i: i64 = 0; var p: i64 = o; while i < len { if p < LCC_ROW_CAP - 2 { d[p] = buf[off + i]; p = p + 1 } i = i + 1 } return p } 62func lcc_bnum(d: *u8, o: i64, v: i64) -> i64 { 63 let t: *u8 = sys_mmap(LCC_NUM_CAP) 64 var x: i64 = v 65 var p: i64 = o 66 if x < 0 { p = lcc_bcat(d, p, "-" as *u8); x = 0 - x } 67 var n: i64 = 0 68 if x == 0 { t[0] = BF_DIGIT0 as u8; n = 1 } 69 while x > 0 { t[n] = (x - (x / LCC_TEN) * LCC_TEN + BF_DIGIT0) as u8; n = n + 1; x = x / LCC_TEN } 70 while n > 0 { n = n - 1; if p < LCC_ROW_CAP - 2 { d[p] = t[n]; p = p + 1 } } 71 return p 72} 73func lcc_bscore(d: *u8, o: i64, v: i64) -> i64 { 74 if v == LBC_NONE { return lcc_bcat(d, o, "-" as *u8) } 75 var p: i64 = lcc_bnum(d, o, v / LCC_TEN) 76 p = lcc_bcat(d, p, "." as *u8) 77 return lcc_bnum(d, p, v - (v / LCC_TEN) * LCC_TEN) 78} 79 80// the leaderboard file through the same two trees the plan is read through 81func lcc_read_lb(dom: *u8, lenp: *i64) -> *u8 { 82 let path: *u8 = sys_mmap(CT_PATH_CAP) 83 ct_build_path(ct_first_published(), dom, LCC_LB_SUFFIX, path) 84 var buf: *u8 = sys_read_file(path, lenp) 85 if lenp[0] > 0 { return buf } 86 ct_build_path(ct_second_published(), dom, LCC_LB_SUFFIX, path) 87 buf = sys_read_file(path, lenp) 88 return buf 89} 90 91// one contender line; the free text (title, then the basis on its own line) comes last so key=value readers anchor 92func lcc_gate_line(pbuf: *u8, pn: i64, k: *i64, q: i64, erank: i64, leader: i64) -> i64 { 93 let fo: *i64 = sys_mmap(LBC_I64) as *i64 94 let ro: i64 = k[q * LBC_KF + LBC_K_ROFF] 95 let re: i64 = bf_line_end(pbuf, pn, ro) 96 let st: i64 = k[q * LBC_KF + LBC_K_STATE] 97 let fl: i64 = k[q * LBC_KF + LBC_K_FLOOR] 98 let rc: i64 = k[q * LBC_KF + LBC_K_REACH] 99 let gp: i64 = k[q * LBC_KF + LBC_K_GATE] 100 lcc_w(" gate rung=" as *u8) 101 let il: i64 = bf_field(pbuf, ro, re, LD_F_R_ID, fo) 102 lcc_wspan(pbuf, fo[0], il) 103 lcc_w(" state=" as *u8); lcc_w(lbc_state_name(st)) 104 lcc_w(" floor=" as *u8); lcc_wscore(fl) 105 if st == LBC_S_GATED { 106 var moves: i64 = 0 107 if rc < erank { moves = 1 } 108 var clears: i64 = 0 109 if fl > leader { clears = 1 } 110 lcc_w(" reaches_rank=" as *u8); lcc_wn(rc) 111 lcc_w(" moves_rank=" as *u8); lcc_wn(moves) 112 lcc_w(" clears_leader=" as *u8); lcc_wn(clears) 113 } 114 lcc_w(" title=" as *u8) 115 var tl: i64 = bf_field(pbuf, ro, re, LD_F_R_TITLE, fo) 116 if tl > LCC_TITLE_MAX { tl = LCC_TITLE_MAX } 117 lcc_wspan(pbuf, fo[0], tl) 118 lcc_w("\n" as *u8) 119 if gp != LBC_NONE { 120 let ge: i64 = bf_line_end(pbuf, pn, gp) 121 let bl: i64 = bf_field(pbuf, gp, ge, LBC_F_G_BASIS, fo) 122 if bl > 0 { lcc_w(" basis: " as *u8); lcc_wspan(pbuf, fo[0], bl); lcc_w("\n" as *u8) } 123 } 124 return 0 125} 126 127// one board: print it, return its verdict 128func lcc_board(dom: *u8, pbuf: *u8, pn: i64, lbuf: *u8, ln: i64, b: *i64, bi: i64, jfd: i64) -> i64 { 129 let board: *u8 = sys_mmap(LBC_NAME_CAP) 130 lbc_span_z(pbuf, b[bi * LBC_BF + LBC_B_NAMEOFF], b[bi * LBC_BF + LBC_B_NAMELEN], board) 131 let nrmax: i64 = bf_count_rows(pbuf, pn, LD_RUNG_TAG) 132 let k: *i64 = sys_mmap((nrmax + 1) * LBC_KF * LBC_I64) as *i64 133 let c: *i64 = sys_mmap(LBC_N_COUNT * LBC_I64) as *i64 134 let v: i64 = lbc_climb(pbuf, pn, lbuf, ln, board, b[bi * LBC_BF + LBC_B_TGTOFF], b[bi * LBC_BF + LBC_B_TGTLEN], k, c) 135 lcc_w("LBCLIMB domain=" as *u8); lcc_w(dom) 136 lcc_w(" board=" as *u8); lcc_w(board) 137 lcc_w(" target=" as *u8); lcc_wspan(pbuf, b[bi * LBC_BF + LBC_B_TGTOFF], b[bi * LBC_BF + LBC_B_TGTLEN]) 138 lcc_w(" rivals=" as *u8); lcc_wn(c[LBC_N_RIVALS]) 139 lcc_w(" leader=" as *u8); lcc_wscore(c[LBC_N_LEADER_X10]) 140 lcc_w(" estate=" as *u8); lcc_wscore(c[LBC_N_ESTATE_X10]) 141 lcc_w(" estate_rank=" as *u8); lcc_wn(c[LBC_N_ESTATE_RANK]) 142 lcc_w("\n" as *u8) 143 // floor order: gated ascending by floor first, then the rest in plan order (selection over the small population) 144 let nk: i64 = c[LBC_N_CONTENDERS] 145 let done: *i64 = sys_mmap((nk + 1) * LBC_I64) as *i64 146 var z: i64 = 0 147 while z < nk { done[z] = 0; z = z + 1 } 148 var left: i64 = c[LBC_N_GATED] 149 while left > 0 { 150 var best: i64 = LBC_NONE 151 var q: i64 = 0 152 while q < nk { 153 if done[q] == 0 { if k[q * LBC_KF + LBC_K_STATE] == LBC_S_GATED { 154 if best == LBC_NONE { best = q } else { if k[q * LBC_KF + LBC_K_FLOOR] < k[best * LBC_KF + LBC_K_FLOOR] { best = q } } 155 } } 156 q = q + 1 157 } 158 if best == LBC_NONE { left = 0 } else { 159 lcc_gate_line(pbuf, pn, k, best, c[LBC_N_ESTATE_RANK], c[LBC_N_LEADER_X10]) 160 done[best] = 1 161 left = left - 1 162 } 163 } 164 var r: i64 = 0 165 while r < nk { 166 if done[r] == 0 { lcc_gate_line(pbuf, pn, k, r, c[LBC_N_ESTATE_RANK], c[LBC_N_LEADER_X10]) } 167 r = r + 1 168 } 169 lcc_w("LBCLIMB-SUM board=" as *u8); lcc_w(board) 170 lcc_w(" contenders=" as *u8); lcc_wn(nk) 171 lcc_w(" gated=" as *u8); lcc_wn(c[LBC_N_GATED]) 172 lcc_w(" ungated=" as *u8); lcc_wn(c[LBC_N_UNGATED]) 173 lcc_w(" nogate=" as *u8); lcc_wn(c[LBC_N_NOGATE]) 174 lcc_w(" badfloor=" as *u8); lcc_wn(c[LBC_N_BADFLOOR]) 175 lcc_w(" sum=" as *u8); lcc_wn(lbc_partition_sum(c)) 176 lcc_w(" stray=" as *u8); lcc_wn(c[LBC_N_STRAY]) 177 lcc_w(" dupgate=" as *u8); lcc_wn(c[LBC_N_DUPGATE]) 178 lcc_w(" dynamic=" as *u8); lcc_wn(c[LBC_N_DYNAMIC]) 179 lcc_w(" best_floor=" as *u8); lcc_wscore(c[LBC_N_BEST_FLOOR_X10]) 180 lcc_w(" best_reach=" as *u8); lcc_wn(c[LBC_N_BEST_REACH]) 181 lcc_w(" movers=" as *u8); lcc_wn(c[LBC_N_MOVERS]) 182 lcc_w(" reachers=" as *u8); lcc_wn(c[LBC_N_REACHERS]) 183 lcc_w(" verdict=" as *u8); lcc_w(lbc_verdict_name(v)); lcc_w("\n" as *u8) 184 if v == LBC_V_SHORT { 185 lcc_w(" SHORT: no declared gate is above the leader " as *u8); lcc_wscore(c[LBC_N_LEADER_X10]) 186 lcc_w("; the best declared floor " as *u8); lcc_wscore(c[LBC_N_BEST_FLOOR_X10]) 187 lcc_w(" reaches rank " as *u8); lcc_wn(c[LBC_N_BEST_REACH]) 188 lcc_w(". The board owes a contender rung whose climbgate floor clears the leader (a score, or the word leader).\n" as *u8) 189 } 190 if jfd >= 0 { 191 let ts: *i64 = sys_mmap(LCC_CLOCK_WORDS * LBC_I64) as *i64 192 ts[0] = 0 193 sys_clock_gettime_real(ts) 194 let row: *u8 = sys_mmap(LCC_ROW_CAP) 195 var o: i64 = lcc_bcat(row, 0, "lbclimb|" as *u8) 196 o = lcc_bnum(row, o, ts[0]); o = lcc_bcat(row, o, "|" as *u8) 197 o = lcc_bcat(row, o, dom); o = lcc_bcat(row, o, "|" as *u8) 198 o = lcc_bcat(row, o, board); o = lcc_bcat(row, o, "|" as *u8) 199 o = lcc_bspan(row, o, pbuf, b[bi * LBC_BF + LBC_B_TGTOFF], b[bi * LBC_BF + LBC_B_TGTLEN]) 200 o = lcc_bcat(row, o, "|leader=" as *u8); o = lcc_bscore(row, o, c[LBC_N_LEADER_X10]) 201 o = lcc_bcat(row, o, "|estate=" as *u8); o = lcc_bscore(row, o, c[LBC_N_ESTATE_X10]) 202 o = lcc_bcat(row, o, "|estate_rank=" as *u8); o = lcc_bnum(row, o, c[LBC_N_ESTATE_RANK]) 203 o = lcc_bcat(row, o, "|contenders=" as *u8); o = lcc_bnum(row, o, nk) 204 o = lcc_bcat(row, o, "|gated=" as *u8); o = lcc_bnum(row, o, c[LBC_N_GATED]) 205 o = lcc_bcat(row, o, "|best_floor=" as *u8); o = lcc_bscore(row, o, c[LBC_N_BEST_FLOOR_X10]) 206 o = lcc_bcat(row, o, "|best_reach=" as *u8); o = lcc_bnum(row, o, c[LBC_N_BEST_REACH]) 207 o = lcc_bcat(row, o, "|movers=" as *u8); o = lcc_bnum(row, o, c[LBC_N_MOVERS]) 208 o = lcc_bcat(row, o, "|reachers=" as *u8); o = lcc_bnum(row, o, c[LBC_N_REACHERS]) 209 o = lcc_bcat(row, o, "|" as *u8); o = lcc_bcat(row, o, lbc_verdict_name(v)) 210 o = lcc_bcat(row, o, "\n" as *u8) 211 let wrote: i64 = sys_write(jfd, row, o) 212 lcc_w(" journal: wrote=" as *u8); lcc_wn(wrote); lcc_w(" of=" as *u8); lcc_wn(o); lcc_w("\n" as *u8) 213 } 214 return v 215} 216 217func main(argc: i64, argv: *i64) -> i64 { 218 if argc <= LCC_ARG_DOMAIN { 219 lcc_w("usage: nx_lbclimb <domain> [board] -- exit 0 REACHES | 1 SHORT or BADTARGET | 3 abstain (NOGATES, NOBOARD, NO-CLIMB, NO-PLAN)\n" as *u8) 220 return LCC_EXIT_USAGE 221 } 222 let dom: *u8 = argv[LCC_ARG_DOMAIN] as *u8 223 let lenp: *i64 = sys_mmap(LBC_I64) as *i64 224 let which: *i64 = sys_mmap(LBC_I64) as *i64 225 lenp[0] = 0 226 let pbuf: *u8 = bf_read_plan(ct_first_published(), ct_second_published(), dom, lenp, which) 227 let pn: i64 = lenp[0] 228 if pn <= 0 { 229 lcc_w("LBCLIMB-VERDICT domain=" as *u8); lcc_w(dom); lcc_w(" plan=UNREADABLE-IN-BOTH-TREES verdict=NO-PLAN\n" as *u8) 230 return LBC_EXIT_ABSTAIN 231 } 232 let llen: *i64 = sys_mmap(LBC_I64) as *i64 233 llen[0] = 0 234 let lbuf: *u8 = lcc_read_lb(dom, llen) 235 var ln: i64 = llen[0] 236 if ln < 0 { ln = 0 } 237 let b: *i64 = sys_mmap(LCC_BOARD_CAP * LBC_BF * LBC_I64) as *i64 238 let nb: i64 = lbc_boards(pbuf, pn, b, LCC_BOARD_CAP) 239 var ran: i64 = 0 240 var n_reach: i64 = 0 241 var n_short: i64 = 0 242 var n_abst: i64 = 0 243 // optional arguments after the domain, in any order: a board filter, and journal=<path> 244 var filt: *u8 = 0 as *u8 245 var jfd: i64 = LCC_NOFD 246 var ai: i64 = LCC_ARG_BOARD 247 while ai < argc { 248 let a: *u8 = argv[ai] as *u8 249 var isj: i64 = 0 250 if bf_slen(a) > LCC_JRNL_PFX_LEN { isj = ld_span_is(a, 0, LCC_JRNL_PFX_LEN, LCC_JRNL_PFX) } 251 if isj == 1 { 252 jfd = sys_openat_append((a as i64 + LCC_JRNL_PFX_LEN) as *u8, LCC_MODE_644) 253 if jfd < 0 { lcc_w("LBCLIMB journal UNWRITABLE: " as *u8); lcc_w((a as i64 + LCC_JRNL_PFX_LEN) as *u8); lcc_w(" -- the run proceeds and says so, it never pretends a row landed\n" as *u8) } 254 } else { filt = a } 255 ai = ai + 1 256 } 257 var bi: i64 = 0 258 while bi < nb { 259 var take: i64 = 1 260 if (filt as i64) != 0 { take = ld_span_is(pbuf, b[bi * LBC_BF + LBC_B_NAMEOFF], b[bi * LBC_BF + LBC_B_NAMELEN], filt) } 261 if take == 1 { 262 let v: i64 = lcc_board(dom, pbuf, pn, lbuf, ln, b, bi, jfd) 263 let ex: i64 = lbc_exit_of(v) 264 if ex == LBC_EXIT_REACHES { n_reach = n_reach + 1 } 265 if ex == LBC_EXIT_SHORT { n_short = n_short + 1 } 266 if ex == LBC_EXIT_ABSTAIN { n_abst = n_abst + 1 } 267 ran = ran + 1 268 } 269 bi = bi + 1 270 } 271 lcc_w("LBCLIMB-VERDICT domain=" as *u8); lcc_w(dom) 272 lcc_w(" boards_declared=" as *u8); lcc_wn(nb) 273 lcc_w(" boards_judged=" as *u8); lcc_wn(ran) 274 lcc_w(" reaches=" as *u8); lcc_wn(n_reach) 275 lcc_w(" short=" as *u8); lcc_wn(n_short) 276 lcc_w(" abstain=" as *u8); lcc_wn(n_abst) 277 if ran == 0 { 278 lcc_w(" verdict=NO-CLIMB (labeled, never refused: a board opts in by declaring a climbboard row)\n" as *u8) 279 return LBC_EXIT_ABSTAIN 280 } 281 if n_short > 0 { lcc_w(" verdict=SHORT\n" as *u8); return LBC_EXIT_SHORT } 282 if n_abst > 0 { lcc_w(" verdict=ABSTAIN\n" as *u8); return LBC_EXIT_ABSTAIN } 283 lcc_w(" verdict=REACHES\n" as *u8) 284 return LBC_EXIT_REACHES 285}