code wiki / _hdl_build / nx_gen_rank.nx
nx_gen_rank.nx source
↩ module page · 138 lines · 6768 B
1// nx_gen_rank.nx -- FULL-SCALE score-and-rank on a SOVEREIGN seg-store PLANE (NOT a .tsv -- sovereign-formats-first
2// law, operator 2026-07-21 "why are you building tsvs again"). Scores live in knowledge/store/genscores- (row = cid<TAB>
3// symmetry_q1000), written via sts_seed (one commit per beat run, NOT per gen -> avoids the seg-store quadratic) and
4// read via sts_load. Verbs:
5// score [batch] -> score up to batch not-yet-scored gallery cids (cid list from the daemon sidecar) INTO the plane.
6// rank [n] -> load the ENTIRE plane, sort by symmetry DESC, return top n (default all). Full-scale, no sampling.
7// Scoring uses the shared symmetry lib. license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9import "nx_image_symmetry_lib.nx"
10import "nx_seg_store.nx"
11import "nx_store_seed_lib.nx"
12const K_MAGIC_1048576: i64 = 1048576
13const K_MAGIC_8192: i64 = 8192
14const K_MAGIC_1024: i64 = 1024
15
16func gr_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8) { if (a[i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 }
17// is `cid` (NUL-term) present as a whole token in buf[0..n)? (cids are unique hashes -> substring match is safe)
18func gr_contains(buf: *u8, n: i64, cid: *u8) -> i64 {
19 var nl: i64 = 0; while cid[nl]!=(0 as u8) { nl=nl+1 }
20 if nl==0 { return 0 }
21 var i: i64 = 0
22 while i + nl <= n {
23 var j: i64 = 0; var ok: i64 = 1
24 while j < nl { if (buf[i+j]&0xff)!=(cid[j]&0xff) { ok=0; j=nl } else { j=j+1 } }
25 if ok==1 { return 1 }
26 i=i+1
27 }
28 return 0
29}
30
31func do_score(batch: i64) -> i64 {
32 let cap: i64 = K_MAGIC_1048576
33 let plane: *u8 = sys_mmap(cap)
34 var pn: i64 = sts_load("knowledge/store/genscores-" as *u8, plane, cap - K_MAGIC_8192)
35 if pn < 0 { pn = 0 }
36 let clp: *i64 = sys_mmap(16) as *i64
37 let cbuf: *u8 = sys_read_file("/volume1/ai/gen/cids.tsv" as *u8, clp)
38 if (cbuf as i64)==0 { sys_write(1, "{\"error\":\"gallery sidecar (cids.tsv) missing\"}\n" as *u8, 46); return 1 }
39 let clen: i64 = clp[0]
40 let cid: *u8 = sys_mmap(128)
41 var scored: i64 = 0
42 var i: i64 = 0; var ls: i64 = 0
43 while i <= clen {
44 var atend: i64 = 0
45 if i==clen { atend=1 } else { if (cbuf[i]&0xff)==10 { atend=1 } }
46 if atend==1 {
47 var cp: i64 = 0; var k: i64 = ls
48 while k < i { let c: i64=cbuf[k]&0xff; if c==9 { k=i } else { if cp<127 { cid[cp]=cbuf[k]; cp=cp+1 } k=k+1 } }
49 cid[cp]=0 as u8
50 if cp > 0 { if scored < batch {
51 if gr_contains(plane, pn, cid)==0 {
52 let sc: i64 = go_score_cid(cid)
53 if sc >= 0 {
54 var m: i64=0; while cid[m]!=(0 as u8){plane[pn]=cid[m]; pn=pn+1; m=m+1}
55 plane[pn]=9 as u8; pn=pn+1; pn=ib_itoa(plane, pn, sc); plane[pn]=10 as u8; pn=pn+1
56 scored = scored + 1
57 }
58 }
59 } }
60 ls = i + 1
61 }
62 i = i + 1
63 }
64 if scored > 0 { if sts_seed("knowledge/store/genscores-" as *u8, plane, pn) < 0 { sys_write(1, "{\"error\":\"plane commit failed\"}\n" as *u8, 31); return 1 } }
65 let out: *u8 = sys_mmap(128); var oo: i64 = ib_cat(out, 0, "{\"scored_this_call\":" as *u8); oo=ib_itoa(out, oo, scored); oo=ib_cat(out, oo, ",\"plane\":\"knowledge/store/genscores-\"}\n" as *u8)
66 sys_write(1, out, oo)
67 return 0
68}
69
70func do_rank(topn: i64) -> i64 {
71 let cap: i64 = K_MAGIC_1048576
72 let sbuf: *u8 = sys_mmap(cap)
73 let slen: i64 = sts_load("knowledge/store/genscores-" as *u8, sbuf, cap)
74 if slen <= 0 { sys_write(1, "{\"ranked\":[],\"note\":\"genscores- plane empty -- run nx_gen_rank score first\"}\n" as *u8, 76); return 0 }
75 let MAXN: i64 = K_MAGIC_1024
76 let cids: *u8 = sys_mmap(MAXN * 80)
77 let scores: *i64 = sys_mmap(MAXN * 8) as *i64
78 let idx: *i64 = sys_mmap(MAXN * 8) as *i64
79 var nc: i64 = 0
80 var i: i64 = 0; var ls: i64 = 0
81 while i <= slen {
82 var atend: i64 = 0
83 if i==slen { atend=1 } else { if (sbuf[i]&0xff)==10 { atend=1 } }
84 if atend==1 {
85 if i > ls { if nc < MAXN {
86 var tab: i64 = 0-1; var k: i64=ls
87 while k < i { if (sbuf[k]&0xff)==9 { tab=k; k=i } else { k=k+1 } }
88 if tab > ls {
89 let dst: *u8 = (cids as i64 + nc*80) as *u8
90 var cp: i64=0; var r: i64=ls
91 while r < tab { if cp<79 { dst[cp]=sbuf[r]; cp=cp+1 } r=r+1 }
92 dst[cp]=0 as u8
93 var v: i64=0; var q: i64=tab+1
94 while q < i { let c: i64=sbuf[q]&0xff; if c>=48 { if c<=57 { v=v*10+(c-48) } } q=q+1 }
95 var dup: i64=0; var z: i64=0
96 while z < nc { if gr_streq((cids as i64 + z*80) as *u8, dst)==1 { dup=1; z=nc } else { z=z+1 } }
97 if dup==0 { scores[nc]=v; idx[nc]=nc; nc=nc+1 }
98 }
99 } }
100 ls = i + 1
101 }
102 i = i + 1
103 }
104 var ii: i64 = 1
105 while ii < nc {
106 let key: i64 = idx[ii]; var jj: i64 = ii-1; var placed: i64 = 0
107 while placed==0 {
108 if jj < 0 { placed=1 }
109 else { if scores[idx[jj]] < scores[key] { idx[jj+1]=idx[jj]; jj=jj-1 } else { placed=1 } }
110 }
111 idx[jj+1]=key; ii=ii+1
112 }
113 var lim: i64 = nc; if topn > 0 { if topn < nc { lim = topn } }
114 let out: *u8 = sys_mmap(MAXN * 96); var oo: i64 = ib_cat(out, 0, "{\"total_scored\":" as *u8); oo=ib_itoa(out, oo, nc); oo=ib_cat(out, oo, ",\"ranked\":[" as *u8)
115 var e: i64 = 0
116 while e < lim {
117 if e > 0 { out[oo]=44 as u8; oo=oo+1 }
118 let ci: *u8 = (cids as i64 + idx[e]*80) as *u8
119 oo = ib_cat(out, oo, "{\"cid\":\"" as *u8)
120 var kk: i64=0; while ci[kk]!=(0 as u8){out[oo]=ci[kk]; oo=oo+1; kk=kk+1}
121 oo = ib_cat(out, oo, "\",\"symmetry_q1000\":" as *u8); oo=ib_itoa(out, oo, scores[idx[e]]); out[oo]=125 as u8; oo=oo+1
122 e = e + 1
123 }
124 oo = ib_cat(out, oo, "]}\n" as *u8)
125 sys_write(1, out, oo)
126 return 0
127}
128
129func main(argc: i64, argv: *i64) -> i64 {
130 // no-arg default = SCORE (the maintain/refresh action) so the ONE-clock beat organ is just this elf, no wrapper.
131 // Explicit "rank" shows the ranking. MCP callers always pass the verb; the clock execs bare -> score.
132 var verb: *u8 = "score" as *u8
133 if argc >= 2 { verb = argv[1] as *u8 }
134 var num: i64 = 0
135 if argc >= 3 { let ns: *u8 = argv[2] as *u8; var i: i64=0; while ns[i]!=(0 as u8){ let c: i64=ns[i]&0xff; if c>=48 { if c<=57 { num=num*10+(c-48) } } i=i+1 } }
136 if gr_streq(verb, "score" as *u8)==1 { var b: i64 = num; if b<=0 { b=20 } return do_score(b) }
137 return do_rank(num)
138}