code wiki / (root) / nx_bright_rewrite_lib.nx

nx_bright_rewrite_lib.nx

buildroot/runtime/nx_bright_rewrite_lib.nx

34055 B612 linesdepth 10pulls 37 transitivereach 2 importersview sourcekind librarytopic bright
docsdependenciesstructsconstsfunctions

about

nx_bright_rewrite_lib.nx -- R0l: the SOVEREIGN reasoning-rewrite run for BRIGHT as a LIBRARY (search R0l, 2026-09-16). WHY. The referee replay (R0o) proved the rewrite lever is real at 1.5B: the authors' own TongSearch-QR-1.5B rewrites score 229 permil through the estate's plain BM25 against the 149 floor. That number was produced by a query another model wrote, so it can never place the estate on the board. This library produces the rewrites on the estate's OWN engine (nx_nofloat_serve_core, the Q8_0 GGUF twin of the MIT weights) with the paper's own prompt, so the queries it writes are the estate's system and nx_beir_eval may score them under the pre-declared accept rule: within 30 permil of the 229 replay, or the port is not the arm. TWO ENGINE DOORS, ONE RUN (operator 2026-09-16: "through mcp and apis and primitives and workflows and agents not wsl ... from the first byte up"): in-process gguf=<path> the model is loaded into this process (nsv_init_i8) -- the laptop lane. remote engine=a.b.c.d:port every prompt is POSTed to a sovereign nx_nofloat_serve on the LAN (POST /gen, the same JSON contract the app page and nx_engine_dispatch use), so a NAS plan row drives a compute node's engine and the NAS never runs the model. The remote door composes nx_http_client (request), nx_http_response_parse (reply) and the serve core's own JSON helpers (nsv_jesc / nsv_jkey / nsv_jstr / nsv_jint) -- one escaper, one key finder, no second ruler. A per-prompt REFUSAL (the engine answered 400 with an err code: too long for its window) is written as the ORIGINAL query and counted as passthrough, exactly as in-process. An ENGINE FAILURE (unreachable, no reply, an unparseable reply, or err=1 which means the request itself was malformed) is NOT a refusal: it ABORTS the split (state ENGINE-DOWN) so a dead node can never manufacture a "complete" file of original queries. IDEMPOTENT RE-RUNS, so a clock agent may call this every beat: * a split whose final queries.<out>.tsv already exists is skipped as COMPLETE-EXISTING (no engine call); * a split is run under a lease (nx_lease_lib, name brightrewrite-<out>-<split>, ttl lease_ttl); a second runner on the same tree reports LEASED-ELSEWHERE and moves on, so two runners take disjoint splits; * rows already in the .part file are skipped by count (the same resume as before); * a run whose every written row was a passthrough does NOT rename to complete (ALL-PASSTHROUGH): an engine that refuses every prompt has produced nothing, and a file of originals must never be scored as rewrites. Per split it appends <root>/<split>/queries.<out>.tsv.part one row per query as each rewrite lands and renames the part to queries.<out>.tsv only when every query of the split has a row. limit>0 is a SMOKE run: it stops after that many queries per split and never renames, so a smoke file can never be mistaken for a full one. Progress: <root>/../../status/bright_rewrite.<out>.progress (or progress=<path>) is truncate-written after every query (a ts that stops moving while the process lives is a hung run, not a slow one). license_tier: ORIGINAL. No hw writes (Rule 26). LIB (no main).

dependencies 5 imports · 2 importers

nx_syscalls.nx nx_nofloat_serve_core.nx nx_http_client.nx nx_http_response_parse.nx nx_lease_lib.nx nx_bright_rewrite_lib.nx nx_bright_rewrite_gate.nx nx_bright_rewrite_run.nx

imports: nx_syscalls.nxnx_nofloat_serve_core.nxnx_http_client.nxnx_http_response_parse.nxnx_lease_lib.nx

imported by: nx_bright_rewrite_gate.nxnx_bright_rewrite_run.nx

structs

none

consts

43const BR_STDOUT: i64 = 1
44const BR_NSPLITS: i64 = 12
45const BR_PATH_CAP: i64 = 1024
46const BR_NUM_CAP: i64 = 32
47const BR_OUT_CAP: i64 = 65536
48const BR_OUT_USE: i64 = 65000
49const BR_GP_SLOTS: i64 = 16
50const BR_META_SLOTS: i64 = 8
51const BR_ST_SLOTS: i64 = 12
52const BR_CFG_SLOTS: i64 = 12
53const BR_ENG_SLOTS: i64 = 8
54const BR_I64: i64 = 8
55const BR_MAXNEW_DEFAULT: i64 = 512 // NSV_MAXNEW in the serve core: a row may ask for less, never more
56const BR_MODE_I8: i64 = 1
57const BR_TEMP_GREEDY: i64 = 0
58const BR_TOPP_PERMIL: i64 = 1000
59const BR_TOPK_DEFAULT: i64 = 64 // the serve daemon's own default; top_k 0 keeps only the argmax, so a sampled run silently decodes GREEDY (measured 2026-09-16: the sampled row was byte-identical to the greedy one)
60const BR_SEED: i64 = 1
61const BR_CHATML: i64 = 1
62const BR_NOSTREAM: i64 = 0
63const BR_NOFD: i64 = 0 - 1
64const BR_TAB: i64 = 9
65const BR_NL: i64 = 10
66const BR_CR: i64 = 13
67const BR_SPACE: i64 = 32
68const BR_SYS_OPENAT: i64 = 257
69const BR_AT_FDCWD: i64 = 0 - 100
70const BR_O_APPEND_CREATE: i64 = 1089 // O_WRONLY|O_CREAT|O_APPEND
71const BR_FILE_MODE: i64 = 420
75const BR_ENGINE_TOKEN_MS_BOUND: i64 = 2000
76const BR_ENGINE_SETUP_S: i64 = 60
77const BR_ENGINE_RESP_CAP: i64 = 135168 // 4096 header reserve + twice BR_OUT_CAP: an escaped reply may double
78const BR_ENGINE_BODY_RESERVE: i64 = 512 // the JSON skeleton around an escaped prompt
79const BR_ENGINE_HOST_CAP: i64 = 24 // "255.255.255.255:65535" NUL
80const BR_HTTP_OK: i64 = 200
81const BR_HTTP_BAD: i64 = 400
82const BR_ENGINE_ERR_MALFORMED: i64 = 1 // the serve's err for a body it could not parse: OUR defect, never a refusal
84const BR_ENG_REFUSED: i64 = 0 - 1 // per-prompt refusal (meta[5] carries the engine's err) -> passthrough row
85const BR_ENG_ABORT: i64 = 0 - 2 // unparseable / unexpected reply -> the split stops
86const BR_ENG_UNREACHABLE: i64 = 0 - 3 // connect, write or an empty reply -> the split stops
88const BR_STATE_PARTIAL: i64 = 0
89const BR_STATE_COMPLETE: i64 = 1
90const BR_STATE_SMOKE: i64 = 2
91const BR_STATE_EXISTING: i64 = 3
92const BR_STATE_LEASED: i64 = 4
93const BR_STATE_ENGINE_DOWN: i64 = 5
94const BR_STATE_ALL_PASSTHROUGH: i64 = 6
95const BR_STATE_LEASE_IO: i64 = 7
96const BR_LEASE_TTL_DEFAULT: i64 = 21600 // 6 h: longer than a split at the slowest measured pace, so a live runner is never stolen from; an orphan after a crash frees itself within a beat
97const BR_LEASE_NAME_PFX: *u8 = "brightrewrite-" as *u8
98const BR_PROMPT_HEAD: *u8 = "Instructions:\n1. Identify the essential problem.\n2. Think step by step to reason and describe what information could be relevant and helpful to address the questions in detail.\n3. Draft an answer with as many thoughts as you have.\nQuery: " as *u8
99const BR_PROMPT_TAIL: *u8 = "\n\n" as *u8
100const BR_PROGRESS_REL: *u8 = "/../../status/bright_rewrite." as *u8
101const BR_PROGRESS_EXT: *u8 = ".progress" as *u8
102const BR_GEN_PATH: *u8 = "/gen" as *u8
103const BR_GEN_CTYPE: *u8 = "application/json" as *u8
112const BR_ENGINE_KIND_NISHI: i64 = 0
113const BR_ENGINE_KIND_LLAMACPP: i64 = 1
114const BR_GEN_PATH_LLAMACPP: *u8 = "/completion" as *u8
115const BR_CHATML_HEAD: *u8 = "<|im_start|>user\n" as *u8
116const BR_CHATML_TAIL: *u8 = "<|im_end|>\n<|im_start|>assistant\n" as *u8
117const BR_PERMIL: i64 = 1000
118const BR_ORACLE_MARK: *u8 = "oracle" as *u8

functions

120func br_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
121func br_puts(s: *u8) -> i64 { let n: i64 = br_slen(s); if n > 0 { sys_write(BR_STDOUT, s, n) } return 0 }
122func br_putn(v: i64) -> i64
called by 2: br_run_splitmain calls 2: sys_mmapsys_write
135func br_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; var o: i64 = off; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } dst[o] = 0 as u8; return o }
136func br_catb(dst: *u8, off: i64, s: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { dst[off + i] = s[i]; i = i + 1 } dst[off + n] = 0 as u8; return off + n }
called by 1: br_run_split
137func br_catn(dst: *u8, off: i64, v: i64) -> i64
149func br_starts(s: *u8, pfx: *u8) -> i64 { var i: i64 = 0; while pfx[i] != (0 as u8) { if s[i] != pfx[i] { return 0 } i = i + 1 } return 1 }
called by 1: main
150func br_streq(a: *u8, b: *u8) -> i64 { let n: i64 = br_slen(a); if n != br_slen(b) { return 0 } var i: i64 = 0; while i < n { if a[i] != b[i] { return 0 } i = i + 1 } return 1 }
called by 2: mainmain calls 1: br_slen
151func br_atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c < 48 { return v } if c > 57 { return v } v = v * 10 + (c - 48); i = i + 1 } return v }
called by 1: main
153func br_find(buf: *u8, n: i64, needle: *u8) -> i64
167func br_split(i: i64) -> *u8
called by 1: main
181func br_path(dst: *u8, root: *u8, split: *u8, tail: *u8) -> i64
called by 2: bg_split_pathbr_run_split calls 1: br_cat
190func br_progress_path(dst: *u8, root: *u8, out: *u8) -> i64
called by 2: mainmain calls 1: br_cat
197func br_count_lines(buf: *u8, n: i64) -> i64 { var c: i64 = 0; var i: i64 = 0; while i < n { if buf[i] == (BR_NL as u8) { c = c + 1 } i = i + 1 } return c }
called by 1: br_run_split
198func br_write_all(fd: i64, buf: *u8, n: i64) -> i64
204func br_progress(path: *u8, split: *u8, done: i64, of: i64, ngen: i64, ms: i64, pass: i64) -> i64
221func br_flatten(s: *u8, n: i64) -> i64
called by 1: br_run_split
235func br_eng_alloc() -> *i64 { let e: *i64 = sys_mmap(BR_ENG_SLOTS * BR_I64) as *i64; e[6] = 0; e[7] = BR_ENGINE_KIND_NISHI; return e }
called by 2: mainmain calls 1: sys_mmap
237func br_cat_permil(dst: *u8, off: i64, v: i64) -> i64
called by 2: mainbr_engine_body_llamacpp calls 1: br_catn
248func br_engine_parse(s: *u8, eng: *i64) -> i64
called by 2: mainmain
277func br_engine_timeout_s(maxnew: i64) -> i64 { return (maxnew * BR_ENGINE_TOKEN_MS_BOUND) / 1000 + BR_ENGINE_SETUP_S }
called by 2: mainmain
279func br_engine_body(dst: *u8, cap: i64, prompt: *u8, pn: i64, maxnew: i64, temp: i64, topp: i64, topk: i64, seed: i64) -> i64
294func br_engine_body_llamacpp(dst: *u8, cap: i64, prompt: *u8, pn: i64, maxnew: i64, temp: i64, topp: i64, topk: i64, seed: i64) -> i64
310func br_engine_parse_resp_llamacpp(resp: *u8, n: i64, outb: *u8, ocap: i64, meta: *i64) -> i64
340func br_engine_parse_resp(resp: *u8, n: i64, outb: *u8, ocap: i64, meta: *i64) -> i64
374func br_engine_gen(eng: *i64, prompt: *u8, pn: i64, maxnew: i64, temp: i64, topp: i64, topk: i64, seed: i64, outb: *u8, ocap: i64, meta: *i64) -> i64
423func br_split_verdict(rows: i64, done: i64, written: i64, pass: i64, limit: i64) -> i64
called by 2: mainbr_run_split
430func br_state_name(s: i64) -> *u8
called by 1: br_run_split
440func br_lease_name(dst: *u8, out: *u8, split: *u8) -> i64
called by 2: mainbr_run_split calls 1: br_cat
447func br_owner(dst: *u8) -> i64 { var o: i64 = br_cat(dst, 0, "pid" as *u8); o = br_catn(dst, o, ls_pid()); return o }
called by 1: br_run_split calls 3: br_catbr_catnls_pid
454func br_run_split(root: *u8, split: *u8, out: *u8, cfg: *i64, eng: *i64, progress: *u8, st: *i64) -> i64