code wiki / (root) / nx_lang_h2h_gate.nx

nx_lang_h2h_gate.nx source

↩ module page · 143 lines · 10276 B

1// nx_lang_h2h_gate.nx -- THE REFEREE for the /compare/lang head-to-head receipt (2026-09-01). 2// Subject: knowledge/compare/lang.bench, the receipt nx_lang_h2h writes, read through the ONE reader every 3// consumer shares (nx_bench_receipt_lib). The gate proves the receipt is a measurement and not a claim: 4// - a reference arm and a NishiLang arm exist and are VALID, every VALID arm printed the reference checksum, 5// every VALID arm carries its run count, min <= median, a positive median, a 64-hex source sha256 and a 6// 64-hex binary sha256, a ratio derived from the reference median, a repro command and a 4-field host line; 7// - the writer's @verdict and the reader's re-derived verdict AGREE (rows edited after writing are caught); 8// - the classifier BITES: a planted checksum mismatch reads VOID, a missing reference reads UNMEASURABLE, an 9// empty receipt reads EMPTY, a forged @verdict over void rows is a writer/reader disagreement. 10// It says NOTHING about whether the numbers are good: that is the page's honest verdict to state and the 11// operator's to accept. A receipt that is absent SKIPs (I could not look), never RED. 12// exit: 0 GREEN . 1 RED . 3 SKIP (gv_verdict) license_tier: ORIGINAL No hw writes (Rule 26). 13import "nx_syscalls.nx" 14import "nx_gate_verdict.nx" 15import "nx_bench_receipt_lib.nx" 16 17const LG_PATH: i64 = 1024 18const LG_FIX: i64 = 8192 19const LG_WORD: i64 = 8 20const LG_SHA_HEX: i64 = 64 21const LG_HOST_FIELDS: i64 = 4 22const LG_PIPE: i64 = 124 23const LG_NX_ARM: *u8 = "nishilang-checked" 24const LG_RATIO_PROBE: i64 = 2020 25const LG_RATIO_PROBE_TEXT: *u8 = "2.02x" 26 27func lg_count_pipes(s: *u8) -> i64 { var c: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { if (s[i] as i64) == LG_PIPE { c = c + 1 } i = i + 1 } return c } 28func lg_is_hex64(s: *u8) -> i64 { 29 var i: i64 = 0 30 while s[i] != (0 as u8) { 31 let c: i64 = s[i] as i64 32 var ok: i64 = 0 33 if c >= 48 { if c <= 57 { ok = 1 } } 34 if c >= 97 { if c <= 102 { ok = 1 } } 35 if ok == 0 { return 0 } 36 i = i + 1 37 } 38 if i == LG_SHA_HEX { return 1 } 39 return 0 40} 41// resolve the live receipt: nishihost root first (buildroot/ prefix), then a buildroot CWD; names which 42func lg_resolve(out: *u8) -> i64 { 43 br_cat(out, 0, "buildroot/knowledge/compare/lang.bench" as *u8) 44 let a: i64 = sys_openat_rd(out) 45 if a >= 0 { sys_close(a); return 1 } 46 br_cat(out, 0, "knowledge/compare/lang.bench" as *u8) 47 let b: i64 = sys_openat_rd(out) 48 if b >= 0 { sys_close(b); return 1 } 49 return 0 50} 51// a fixture receipt built at RUNTIME (a detector that scans source finds its own fixture); chk2 is the 52// second arm's checksum, withref=0 omits the @ref line, verdictword is what the fake writer wrote 53func lg_fixture(dst: *u8, chk2: i64, withref: i64, verdictword: *u8) -> i64 { 54 var o: i64 = br_cat(dst, 0, "@title fixture\n@workload fixture\n@host fx|cpu|Linux 0|nproc=1\n@runs 3\n" as *u8) 55 if withref == 1 { o = br_cat(dst, o, "@ref a\n" as *u8) } 56 o = br_cat(dst, o, "@asof 1788307830\n@repro none\n@writer fixture\n@verdict " as *u8); o = br_cat(dst, o, verdictword); o = br_put(dst, o, 10) 57 o = br_cat(dst, o, "arm|a|ta|va|sa|0000000000000000000000000000000000000000000000000000000000000000|10|0000000000000000000000000000000000000000000000000000000000000000|3|100|110|777|VALID|n\n" as *u8) 58 o = br_cat(dst, o, "arm|b|tb|vb|sb|0000000000000000000000000000000000000000000000000000000000000000|10|0000000000000000000000000000000000000000000000000000000000000000|3|200|220|" as *u8) 59 o = br_catn(dst, o, chk2) 60 o = br_cat(dst, o, "|VALID|n\n" as *u8) 61 return o 62} 63 64func main(argc: i64, argv: *i64) -> i64 { 65 let ctr: *i64 = gv_ctr() 66 gv_head("NX-LANG-H2H-GATE: the /compare/lang head-to-head receipt is a measurement, and its reader bites" as *u8) 67 // ---- in-process teeth over the shared classifier (no receipt needed for these) ---- 68 let hdr: *i64 = sys_mmap(BR_H_N * LG_WORD) as *i64 69 let arms: *i64 = sys_mmap(BR_MAXARMS * BR_STRIDE * LG_WORD) as *i64 70 let fx: *u8 = sys_mmap(LG_FIX) 71 var fn: i64 = lg_fixture(fx, 777, 1, "VALID" as *u8) 72 var n: i64 = br_parse(fx, fn, hdr, arms) 73 let v_good: i64 = br_verdict(hdr, arms, n) 74 gv_check("fixture-two-agreeing-arms-parse-to-2" as *u8, (n == 2) as i64, ctr) 75 gv_check("fixture-agreeing-checksums-read-VALID" as *u8, (v_good == BR_V_VALID) as i64, ctr) 76 gv_check("fixture-ratio-of-arm-b-is-2000-permil" as *u8, (arms[1 * BR_STRIDE + BR_A_RATIO] == 2000) as i64, ctr) 77 fn = lg_fixture(fx, 778, 1, "VALID" as *u8) 78 n = br_parse(fx, fn, hdr, arms) 79 let v_bad: i64 = br_verdict(hdr, arms, n) 80 gv_bite("neg-control-planted-checksum-mismatch-reads-VOID" as *u8, (v_bad == BR_V_VOID) as i64, (v_good == BR_V_VOID) as i64, ctr) 81 gv_check("neg-control-mismatched-arm-status-flips-to-VOID" as *u8, (arms[1 * BR_STRIDE + BR_A_STATUS] == BR_ST_VOID) as i64, ctr) 82 gv_check("neg-control-void-arm-carries-no-ratio" as *u8, (arms[1 * BR_STRIDE + BR_A_RATIO] < 0) as i64, ctr) 83 gv_check("neg-control-forged-VALID-over-void-rows-disagrees-with-the-reader" as *u8, (hdr[BR_H_WRITTEN_VERDICT] != v_bad) as i64, ctr) 84 fn = lg_fixture(fx, 777, 0, "VALID" as *u8) 85 n = br_parse(fx, fn, hdr, arms) 86 gv_check("neg-control-missing-ref-arm-reads-UNMEASURABLE" as *u8, (br_verdict(hdr, arms, n) == BR_V_UNMEASURABLE) as i64, ctr) 87 br_init(hdr, arms) 88 gv_check("neg-control-empty-receipt-reads-EMPTY" as *u8, (br_verdict(hdr, arms, 0) == BR_V_EMPTY) as i64, ctr) 89 let rt: *u8 = sys_mmap(32) 90 br_ratio_text(LG_RATIO_PROBE, rt, 0) 91 gv_check("ratio-text-2020-permil-renders-2.02x" as *u8, br_streq(rt, LG_RATIO_PROBE_TEXT), ctr) 92 93 // ---- the live receipt ---- 94 let path: *u8 = sys_mmap(LG_PATH) 95 let have: i64 = lg_resolve(path) 96 gv_puts(" receipt=" as *u8); if have == 1 { gv_puts(path) } else { gv_puts("ABSENT" as *u8) } gv_puts("\n" as *u8) 97 if gv_need("live receipt knowledge/compare/lang.bench (nishihost root or buildroot CWD)" as *u8, have, ctr) == 1 { 98 let ln: i64 = br_load(path, hdr, arms) 99 gv_subjects("arm rows in the live receipt" as *u8, ln, ctr) 100 if ln > 0 { 101 let lv: i64 = br_verdict(hdr, arms, ln) 102 gv_puts(" live verdict=" as *u8); gv_puts(br_verdict_name(lv)); gv_puts(" written=" as *u8); gv_puts(br_verdict_name(hdr[BR_H_WRITTEN_VERDICT])); gv_puts(" ref=" as *u8); gv_puts(hdr[BR_H_REF] as *u8); gv_puts(" runs=" as *u8); gv_num(hdr[BR_H_RUNS]); gv_puts("\n" as *u8) 103 gv_check("live-at-least-two-arms" as *u8, (ln >= 2) as i64, ctr) 104 gv_check("live-reference-arm-named-and-VALID" as *u8, (lv != BR_V_UNMEASURABLE) as i64, ctr) 105 gv_check("live-every-VALID-arm-printed-the-reference-checksum" as *u8, (lv != BR_V_VOID) as i64, ctr) 106 gv_check("live-writer-and-reader-verdicts-agree" as *u8, (hdr[BR_H_WRITTEN_VERDICT] == lv) as i64, ctr) 107 gv_check("live-repro-command-present" as *u8, (br_slen(hdr[BR_H_REPRO] as *u8) > 0) as i64, ctr) 108 gv_check("live-host-line-carries-hostname-cpu-kernel-nproc" as *u8, (lg_count_pipes(hdr[BR_H_HOST] as *u8) == LG_HOST_FIELDS - 1) as i64, ctr) 109 gv_check("live-runs-declared-at-least-3" as *u8, (hdr[BR_H_RUNS] >= 3) as i64, ctr) 110 var nx_valid: i64 = 0 111 var c_valid: i64 = 0; var c_void: i64 = 0; var c_unm: i64 = 0; var c_bf: i64 = 0; var c_rf: i64 = 0; var c_unk: i64 = 0 112 var all_runs: i64 = 1; var all_order: i64 = 1; var all_pos: i64 = 1; var all_sha: i64 = 1; var all_ratio: i64 = 1 113 var i: i64 = 0 114 while i < ln { 115 let b: i64 = i * BR_STRIDE 116 let st: i64 = arms[b + BR_A_STATUS] 117 if st == BR_ST_VALID { c_valid = c_valid + 1 } else { if st == BR_ST_VOID { c_void = c_void + 1 } else { 118 if st == BR_ST_UNMEASURABLE { c_unm = c_unm + 1 } else { if st == BR_ST_BUILDFAIL { c_bf = c_bf + 1 } else { 119 if st == BR_ST_RUNFAIL { c_rf = c_rf + 1 } else { c_unk = c_unk + 1 } } } } } 120 if st == BR_ST_VALID { 121 if br_streq(arms[b + BR_A_NAME] as *u8, LG_NX_ARM) == 1 { nx_valid = 1 } 122 if arms[b + BR_A_RUNS] != hdr[BR_H_RUNS] { all_runs = 0 } 123 if arms[b + BR_A_MIN] > arms[b + BR_A_MED] { all_order = 0 } 124 if arms[b + BR_A_MED] <= 0 { all_pos = 0 } 125 if lg_is_hex64(arms[b + BR_A_SRCSHA] as *u8) == 0 { all_sha = 0 } 126 if lg_is_hex64(arms[b + BR_A_BINSHA] as *u8) == 0 { all_sha = 0 } 127 if arms[b + BR_A_RATIO] < 0 { all_ratio = 0 } 128 } 129 gv_puts(" " as *u8); gv_puts(arms[b + BR_A_NAME] as *u8); gv_puts(" " as *u8); gv_puts(br_status_name(st)); gv_puts(" median_us=" as *u8); gv_num(arms[b + BR_A_MED]); gv_puts(" ratio_permil=" as *u8); gv_num(arms[b + BR_A_RATIO]); gv_puts("\n" as *u8) 130 i = i + 1 131 } 132 gv_puts(" partition valid=" as *u8); gv_num(c_valid); gv_puts(" void=" as *u8); gv_num(c_void); gv_puts(" unmeasurable=" as *u8); gv_num(c_unm); gv_puts(" build_fail=" as *u8); gv_num(c_bf); gv_puts(" run_fail=" as *u8); gv_num(c_rf); gv_puts(" unknown=" as *u8); gv_num(c_unk); gv_puts(" arms=" as *u8); gv_num(ln); gv_puts("\n" as *u8) 133 gv_check("live-status-partition-sums-to-arms" as *u8, (c_valid + c_void + c_unm + c_bf + c_rf + c_unk == ln) as i64, ctr) 134 gv_check("live-nishilang-arm-present-and-VALID" as *u8, nx_valid, ctr) 135 gv_check("live-every-VALID-arm-ran-the-declared-run-count" as *u8, all_runs, ctr) 136 gv_check("live-every-VALID-arm-min-not-above-median" as *u8, all_order, ctr) 137 gv_check("live-every-VALID-arm-median-positive" as *u8, all_pos, ctr) 138 gv_check("live-every-VALID-arm-carries-64-hex-source-and-binary-sha256" as *u8, all_sha, ctr) 139 gv_check("live-every-VALID-arm-carries-a-derived-ratio" as *u8, all_ratio, ctr) 140 } 141 } 142 return gv_verdict("nx_lang_h2h_gate" as *u8, ctr, "the reader bites on planted mismatches and the live receipt is a complete, self-consistent measurement; the numbers themselves are the page's to state honestly" as *u8) 143}