code wiki / _hdl_build / nx_textcut_gate.nx

nx_textcut_gate.nx source

↩ module page · 106 lines · 6527 B

1// nx_textcut_gate.nx -- GATE for nx_textcut (token-boundary-safe cutting). 2// THE FIXTURE IS THE PRODUCTION DEFECT, NOT A SYNTHETIC ONE: the title string below is the document 3// nishifamily.com/search rendered on 2026-08-25, and byte 72 of it -- the live renderer's title cap -- 4// falls on the 'F' of "REFPROP". The live SERP served the title "...Database (RE" and then a snippet 5// BEGINNING "FPROP) Version 9". Every tooth here is checked against that measured behaviour. 6// license_tier: ORIGINAL 7import "nx_textcut.nx" 8import "nx_gate_verdict.nx" 9 10const TG_LIVE_TITLE_CAP: i64 = 72 // the cap the LIVE renderer used when it produced the defect 11const TG_BIGCAP: i64 = 4096 12const TG_CH_V: i64 = 86 13const TG_CH_e: i64 = 101 14const TG_CH_a: i64 = 97 15 16func tg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 17func tg_not(v: i64) -> i64 { if v == 0 { return 1 } return 0 } 18func tg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 } 19// explicit predicates: this dialect is only PROVEN to accept a comparison in a condition, never as a 20// value passed to a call. Writing tg_eq(l <= cap, 1) would be betting a build cycle on an unverified 21// language feature, and a tooth that fails to COMPILE proves nothing about the subject. 22func tg_le(a: i64, b: i64) -> i64 { if a <= b { return 1 } return 0 } 23func tg_gt(a: i64, b: i64) -> i64 { if a > b { return 1 } return 0 } 24func tg_show(label: *u8, v: i64) -> i64 { gv_puts(label); gv_num(v); gv_puts("\n" as *u8); return 0 } 25 26func main(argc: i64, argv: *i64) -> i64 { 27 gv_head("=== nx_textcut gate -- token-boundary-safe cutting (fixture = the live 2026-08-25 defect) ===" as *u8) 28 let ctr: *i64 = gv_ctr() 29 let bx: *i64 = sys_mmap(64) as *i64 30 31 let rp: *u8 = "NIST Reference Fluid Thermodynamic and Transport Properties Database (REFPROP) Version 9 - SRD 23" as *u8 32 let rn: i64 = tg_len(rp) 33 34 // ---- ANTI-VACUITY FIRST: prove the fixture can actually EXHIBIT the defect. If this tooth fails, 35 // every tooth below is passing on a case the bug could never have occurred in. 36 gv_check("fixture-reached-naive-cut-at-live-cap-DOES-split-a-token" as *u8, tc_splits_token(rp, rn, TG_LIVE_TITLE_CAP), ctr) 37 38 // ---- the cut itself 39 tc_stats_reset() 40 let l: i64 = tc_cut(rp, rn, TG_LIVE_TITLE_CAP, bx) 41 tg_show(" tc_cut -> " as *u8, l) 42 gv_check("cut-result-does-not-split-a-token" as *u8, tg_not(tc_splits_token(rp, rn, l)), ctr) 43 gv_check("cut-never-exceeds-the-caller-cap" as *u8, tg_eq(l <= TG_LIVE_TITLE_CAP, 1), ctr) 44 gv_check("cut-is-not-degenerate-zero" as *u8, tg_eq(l > 0, 1), ctr) 45 gv_check("cut-on-a-splittable-fixture-is-declared-EXACT" as *u8, tg_eq(bx[0], 1), ctr) 46 47 // ---- the trim: a truncated span must not end on a dangling opener. Cutting at the longest 48 // non-splitting boundary yields "...Database (" -- correct but still visibly broken. 49 let lt: i64 = tc_cut_trim(rp, rn, TG_LIVE_TITLE_CAP, bx) 50 tg_show(" tc_cut_trim -> " as *u8, lt) 51 gv_check("trimmed-title-ends-on-a-word-byte" as *u8, tc_is_wordch(rp[lt - 1] as i64), ctr) 52 gv_check("trimmed-title-ends-with-the-word-Database" as *u8, tg_eq(rp[lt - 1] as i64, TG_CH_e), ctr) 53 gv_check("trim-never-lengthens-the-cut" as *u8, tg_eq(lt <= l, 1), ctr) 54 55 // ---- NEG-CONTROL: a span that FITS must come back byte-identical, trailing punctuation included. 56 // Without this, a trim that always ran would silently eat authors' sentence-final periods and every 57 // tooth above would still pass. 58 let fits: *u8 = "Hello world." as *u8 59 let fn2: i64 = tg_len(fits) 60 let lf: i64 = tc_cut_trim(fits, fn2, TG_BIGCAP, bx) 61 gv_check("neg-control-span-that-fits-is-returned-whole-with-its-period" as *u8, tg_eq(lf, fn2), ctr) 62 63 // ---- the OTHER half: a span that BEGINS mid-token ("FPROP) Version 9") 64 let s: i64 = tc_start(rp, rn, TG_LIVE_TITLE_CAP) 65 tg_show(" tc_start -> " as *u8, s) 66 gv_check("start-moves-off-a-mid-token-offset" as *u8, tg_eq(s > TG_LIVE_TITLE_CAP, 1), ctr) 67 gv_check("start-lands-on-a-word-byte" as *u8, tc_is_wordch(rp[s] as i64), ctr) 68 gv_check("start-skips-the-orphaned-close-paren-and-lands-on-Version" as *u8, tg_eq(rp[s] as i64, TG_CH_V), ctr) 69 70 // ---- NEG-CONTROL: a CLEAN offset must not be over-skipped. If tc_start always advanced to the next 71 // word it would silently eat a whole token here and the tooth above could not tell the difference. 72 let clean: i64 = tc_start(rp, rn, 0) 73 gv_check("neg-control-clean-offset-is-not-advanced-past-its-own-token" as *u8, tg_eq(clean, 0), ctr) 74 75 // ---- UTF-8: a multi-byte codepoint can never be split, because a cut inside one is a cut inside a 76 // token. This closes a gap nx_search_snippet_extract declares in its own V2 SCOPE and never shipped. 77 let u: *u8 = sys_mmap(TG_BIGCAP) 78 u[0] = TG_CH_a as u8 79 u[1] = TG_CH_a as u8 80 u[2] = 195 as u8 // lead byte of U+00E9 81 u[3] = 169 as u8 // continuation byte of U+00E9 82 u[4] = TG_CH_a as u8 83 gv_check("fixture-reached-cap-3-would-land-INSIDE-the-codepoint" as *u8, tc_splits_token(u, 5, 3), ctr) 84 let lu: i64 = tc_cut(u, 5, 3, bx) 85 gv_check("utf8-cut-never-lands-between-lead-and-continuation" as *u8, tg_not(tg_eq(lu, 3)), ctr) 86 87 // ---- THE HARD CUT IS DECLARED, NOT HIDDEN. One unbroken token longer than the cap has no boundary 88 // to retreat to; the contract is that we say so rather than pretend the cut was clean. 89 let big: *u8 = sys_mmap(TG_BIGCAP) 90 var i: i64 = 0 91 while i < 200 { big[i] = TG_CH_a as u8; i = i + 1 } 92 tc_stats_reset() 93 let lb: i64 = tc_cut(big, 200, TG_LIVE_TITLE_CAP, bx) 94 gv_check("unbroken-token-cut-is-declared-INEXACT" as *u8, tg_eq(bx[0], 0), ctr) 95 gv_check("unbroken-token-cut-still-returns-the-full-cap" as *u8, tg_eq(lb, TG_LIVE_TITLE_CAP), ctr) 96 gv_check("hardcut-counter-announces-the-imprecision" as *u8, tg_eq(tc_hardcuts(), 1), ctr) 97 98 // ---- NEG-CONTROL on the announce: the clean fixture must NOT be counted as a hard cut, or the 99 // counter would report imprecision that never happened and nobody could trust the rate. 100 tc_stats_reset() 101 tc_cut(rp, rn, TG_LIVE_TITLE_CAP, bx) 102 gv_check("neg-control-clean-cut-does-NOT-increment-hardcuts" as *u8, tg_eq(tc_hardcuts(), 0), ctr) 103 gv_check("clean-cut-DOES-increment-the-cuts-counter" as *u8, tg_eq(tc_cuts(), 1), ctr) 104 105 return gv_verdict("nx_textcut_gate" as *u8, ctr, "token-boundary cutting proven against the measured live SERP defect" as *u8) 106}