nx_bright_rewrite_lib.nx
buildroot/runtime/nx_bright_rewrite_lib.nx
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
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
| 43 | const BR_STDOUT: i64 = 1 |
| 44 | const BR_NSPLITS: i64 = 12 |
| 45 | const BR_PATH_CAP: i64 = 1024 |
| 46 | const BR_NUM_CAP: i64 = 32 |
| 47 | const BR_OUT_CAP: i64 = 65536 |
| 48 | const BR_OUT_USE: i64 = 65000 |
| 49 | const BR_GP_SLOTS: i64 = 16 |
| 50 | const BR_META_SLOTS: i64 = 8 |
| 51 | const BR_ST_SLOTS: i64 = 12 |
| 52 | const BR_CFG_SLOTS: i64 = 12 |
| 53 | const BR_ENG_SLOTS: i64 = 8 |
| 54 | const BR_I64: i64 = 8 |
| 55 | const BR_MAXNEW_DEFAULT: i64 = 512 // NSV_MAXNEW in the serve core: a row may ask for less, never more |
| 56 | const BR_MODE_I8: i64 = 1 |
| 57 | const BR_TEMP_GREEDY: i64 = 0 |
| 58 | const BR_TOPP_PERMIL: i64 = 1000 |
| 59 | const 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) |
| 60 | const BR_SEED: i64 = 1 |
| 61 | const BR_CHATML: i64 = 1 |
| 62 | const BR_NOSTREAM: i64 = 0 |
| 63 | const BR_NOFD: i64 = 0 - 1 |
| 64 | const BR_TAB: i64 = 9 |
| 65 | const BR_NL: i64 = 10 |
| 66 | const BR_CR: i64 = 13 |
| 67 | const BR_SPACE: i64 = 32 |
| 68 | const BR_SYS_OPENAT: i64 = 257 |
| 69 | const BR_AT_FDCWD: i64 = 0 - 100 |
| 70 | const BR_O_APPEND_CREATE: i64 = 1089 // O_WRONLY|O_CREAT|O_APPEND |
| 71 | const BR_FILE_MODE: i64 = 420 |
| 75 | const BR_ENGINE_TOKEN_MS_BOUND: i64 = 2000 |
| 76 | const BR_ENGINE_SETUP_S: i64 = 60 |
| 77 | const BR_ENGINE_RESP_CAP: i64 = 135168 // 4096 header reserve + twice BR_OUT_CAP: an escaped reply may double |
| 78 | const BR_ENGINE_BODY_RESERVE: i64 = 512 // the JSON skeleton around an escaped prompt |
| 79 | const BR_ENGINE_HOST_CAP: i64 = 24 // "255.255.255.255:65535" NUL |
| 80 | const BR_HTTP_OK: i64 = 200 |
| 81 | const BR_HTTP_BAD: i64 = 400 |
| 82 | const BR_ENGINE_ERR_MALFORMED: i64 = 1 // the serve's err for a body it could not parse: OUR defect, never a refusal |
| 84 | const BR_ENG_REFUSED: i64 = 0 - 1 // per-prompt refusal (meta[5] carries the engine's err) -> passthrough row |
| 85 | const BR_ENG_ABORT: i64 = 0 - 2 // unparseable / unexpected reply -> the split stops |
| 86 | const BR_ENG_UNREACHABLE: i64 = 0 - 3 // connect, write or an empty reply -> the split stops |
| 88 | const BR_STATE_PARTIAL: i64 = 0 |
| 89 | const BR_STATE_COMPLETE: i64 = 1 |
| 90 | const BR_STATE_SMOKE: i64 = 2 |
| 91 | const BR_STATE_EXISTING: i64 = 3 |
| 92 | const BR_STATE_LEASED: i64 = 4 |
| 93 | const BR_STATE_ENGINE_DOWN: i64 = 5 |
| 94 | const BR_STATE_ALL_PASSTHROUGH: i64 = 6 |
| 95 | const BR_STATE_LEASE_IO: i64 = 7 |
| 96 | const 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 |
| 97 | const BR_LEASE_NAME_PFX: *u8 = "brightrewrite-" as *u8 |
| 98 | const 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 |
| 99 | const BR_PROMPT_TAIL: *u8 = "\n\n" as *u8 |
| 100 | const BR_PROGRESS_REL: *u8 = "/../../status/bright_rewrite." as *u8 |
| 101 | const BR_PROGRESS_EXT: *u8 = ".progress" as *u8 |
| 102 | const BR_GEN_PATH: *u8 = "/gen" as *u8 |
| 103 | const BR_GEN_CTYPE: *u8 = "application/json" as *u8 |
| 112 | const BR_ENGINE_KIND_NISHI: i64 = 0 |
| 113 | const BR_ENGINE_KIND_LLAMACPP: i64 = 1 |
| 114 | const BR_GEN_PATH_LLAMACPP: *u8 = "/completion" as *u8 |
| 115 | const BR_CHATML_HEAD: *u8 = "<|im_start|>user\n" as *u8 |
| 116 | const BR_CHATML_TAIL: *u8 = "<|im_end|>\n<|im_start|>assistant\n" as *u8 |
| 117 | const BR_PERMIL: i64 = 1000 |
| 118 | const BR_ORACLE_MARK: *u8 = "oracle" as *u8 |
functions
| 120 | func br_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } |
| 121 | func br_puts(s: *u8) -> i64 { let n: i64 = br_slen(s); if n > 0 { sys_write(BR_STDOUT, s, n) } return 0 } |
| 122 | func br_putn(v: i64) -> i64 |
| 135 | func 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 } |
| 136 | func 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 |
| 137 | func br_catn(dst: *u8, off: i64, v: i64) -> i64 |
| 149 | func 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 |
| 150 | func 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 } |
| 151 | func 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 |
| 153 | func br_find(buf: *u8, n: i64, needle: *u8) -> i64 |
| 167 | func br_split(i: i64) -> *u8 called by 1: main |
| 181 | func br_path(dst: *u8, root: *u8, split: *u8, tail: *u8) -> i64 |
| 190 | func br_progress_path(dst: *u8, root: *u8, out: *u8) -> i64 |
| 197 | func 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 |
| 198 | func br_write_all(fd: i64, buf: *u8, n: i64) -> i64 |
| 204 | func br_progress(path: *u8, split: *u8, done: i64, of: i64, ngen: i64, ms: i64, pass: i64) -> i64 |
| 221 | func br_flatten(s: *u8, n: i64) -> i64 called by 1: br_run_split |
| 235 | func 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 } |
| 237 | func br_cat_permil(dst: *u8, off: i64, v: i64) -> i64 |
| 248 | func br_engine_parse(s: *u8, eng: *i64) -> i64 |
| 277 | func br_engine_timeout_s(maxnew: i64) -> i64 { return (maxnew * BR_ENGINE_TOKEN_MS_BOUND) / 1000 + BR_ENGINE_SETUP_S } |
| 279 | func br_engine_body(dst: *u8, cap: i64, prompt: *u8, pn: i64, maxnew: i64, temp: i64, topp: i64, topk: i64, seed: i64) -> i64 |
| 294 | func br_engine_body_llamacpp(dst: *u8, cap: i64, prompt: *u8, pn: i64, maxnew: i64, temp: i64, topp: i64, topk: i64, seed: i64) -> i64 |
| 310 | func br_engine_parse_resp_llamacpp(resp: *u8, n: i64, outb: *u8, ocap: i64, meta: *i64) -> i64 called by 2: mainbr_engine_gen calls 6: nx_http_resp_allocnx_http_response_parsebr_findnsv_jkeynsv_jstrnsv_jint |
| 340 | func br_engine_parse_resp(resp: *u8, n: i64, outb: *u8, ocap: i64, meta: *i64) -> i64 called by 2: mainbr_engine_gen calls 5: nx_http_resp_allocnx_http_response_parsensv_jintnsv_jkeynsv_jstr |
| 374 | func 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 called by 1: br_run_split calls 13: sys_mmapbr_engine_body_llamacppbr_engine_bodybr_catnbr_slensys_socket+7 |
| 423 | func br_split_verdict(rows: i64, done: i64, written: i64, pass: i64, limit: i64) -> i64 |
| 430 | func br_state_name(s: i64) -> *u8 called by 1: br_run_split |
| 440 | func br_lease_name(dst: *u8, out: *u8, split: *u8) -> i64 |
| 447 | func br_owner(dst: *u8) -> i64 { var o: i64 = br_cat(dst, 0, "pid" as *u8); o = br_catn(dst, o, ls_pid()); return o } |
| 454 | func br_run_split(root: *u8, split: *u8, out: *u8, cfg: *i64, eng: *i64, progress: *u8, st: *i64) -> i64 |