nx_dr_elo_cli.nx source
↩ module page · 81 lines · 3879 B
1// nx_dr_elo_cli.nx -- the callable MCP/API surface for the sovereign Elo tournament
2// ranker (DR-1). Feeds pairwise-critique match outcomes through the deterministic
3// integer Elo engine and emits the ranking as JSON. Any caller -- a research
4// conductor (DR-4), the Reflection/Critic (DR-2), a workstream, a session -- ranks
5// candidate research vectors without re-implementing Elo; sovereign, integer, same
6// bits on every machine.
7// nx_dr_elo <n> [a b o]... n = #players (0..n-1); each match = 3 ints:
8// a,b = player ids, o = A's score permille (1000/500/0)
9// e.g. nx_dr_elo 3 2 0 1000 2 1 1000 0 1 1000
10// -> {"tool":"nx_dr_elo","n":3,"matches":3,...,"ranking":[{"rank":1,"id":2,...}]}
11// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
12import "nx_dr_elo.nx"
13import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
14import "nx_syscalls.nx"
15const K_MAGIC_1500: i64 = 1500
16
17func el_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
18func el_q() -> i64 { let b: *u8 = sys_mmap(1); b[0] = 34 as u8; sys_write(1, b, 1); return 0 }
19func el_key(name: *u8) -> i64 { el_q(); el_puts(name); el_q(); el_puts(":" as *u8); return 0 }
20// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
21// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
22// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
23// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
24func el_num(v: i64) -> i64 { nxi_out(v); return 0 }
25func el_atoi(a: *u8) -> i64 {
26 var v: i64 = 0; var i: i64 = 0
27 while a[i] != (0 as u8) {
28 let c: i64 = a[i] as i64
29 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48) } }
30 i = i + 1
31 }
32 return v
33}
34
35func main(argc: i64, argv: *i64) -> i64 {
36 if argc < 2 { el_puts("{" as *u8); el_key("error" as *u8); el_q(); el_puts("usage: nx_dr_elo <n> [a b o]..." as *u8); el_q(); el_puts("}\n" as *u8); sys_exit(2); return 2 }
37 let n: i64 = el_atoi(argv[1] as *u8)
38 if n <= 0 { el_puts("{" as *u8); el_key("error" as *u8); el_q(); el_puts("n must be > 0" as *u8); el_q(); el_puts("}\n" as *u8); sys_exit(1); return 1 }
39
40 let rat: *i64 = sys_mmap(n * 8) as *i64
41 var i: i64 = 0
42 while i < n { rat[i] = K_MAGIC_1500; i = i + 1 }
43
44 let m: i64 = (argc - 2) / 3
45 let ma: *i64 = sys_mmap((m + 1) * 8) as *i64
46 let mb: *i64 = sys_mmap((m + 1) * 8) as *i64
47 let mo: *i64 = sys_mmap((m + 1) * 8) as *i64
48 var t: i64 = 0
49 while t < m {
50 ma[t] = el_atoi(argv[2 + t * 3] as *u8)
51 mb[t] = el_atoi(argv[2 + t * 3 + 1] as *u8)
52 mo[t] = el_atoi(argv[2 + t * 3 + 2] as *u8)
53 t = t + 1
54 }
55
56 let tab: *i64 = el_make_etab()
57 el_run_tournament(rat, n, ma, mb, mo, m, 32, tab)
58 let ord: *i64 = sys_mmap(n * 8) as *i64
59 el_rank(rat, n, ord)
60
61 el_puts("{" as *u8)
62 el_key("tool" as *u8); el_q(); el_puts("nx_dr_elo" as *u8); el_q(); el_puts("," as *u8)
63 el_key("n" as *u8); el_num(n); el_puts("," as *u8)
64 el_key("matches" as *u8); el_num(m); el_puts("," as *u8)
65 el_key("k" as *u8); el_num(32); el_puts("," as *u8)
66 el_key("ranking" as *u8); el_puts("[" as *u8)
67 var r: i64 = 0
68 while r < n {
69 if r > 0 { el_puts("," as *u8) }
70 el_puts("{" as *u8)
71 el_key("rank" as *u8); el_num(r + 1); el_puts("," as *u8)
72 el_key("id" as *u8); el_num(ord[r]); el_puts("," as *u8)
73 el_key("rating" as *u8); el_num(rat[ord[r]])
74 el_puts("}" as *u8)
75 r = r + 1
76 }
77 el_puts("]," as *u8)
78 el_key("note" as *u8); el_q(); el_puts("deterministic integer Elo; ties->lower id; K=32; sovereign no-float" as *u8); el_q()
79 el_puts("}\n" as *u8)
80 sys_exit(0); return 0
81}