code wiki / _hdl_build / nx_docprose_gate.nx

nx_docprose_gate.nx source

↩ module page · 131 lines · 9037 B

1// nx_docprose_gate.nx -- GATE for nx_docprose (where does a stored document's real content begin?). 2// THE FIXTURES ARE THE PRODUCTION DEFECT: the two titles below were served by nishifamily.com/search on 3// 2026-08-25 -- an HTTP status line and a raw markup tag, rendered as document titles because the stored 4// evidence-mirror captures carry their wire bytes intact and the renderer faithfully took "the first 5// non-empty line". Every acceptance tooth is paired with a neg-control that must NOT fire, because a 6// front-matter skipper that skips too much is the same defect pointing the other way. 7// license_tier: ORIGINAL 8import "nx_docprose.nx" 9import "nx_gate_verdict.nx" 10 11const DG_CAP: i64 = 72 // the live renderer's title cap -- the cap under which the defect was seen 12const DG_BIGCAP: i64 = 4096 13const DG_CH_LT: i64 = 60 // '<' 14const DG_CH_BANG: i64 = 33 // '!' 15 16func dg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 17func dg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 } 18func dg_gt(a: i64, b: i64) -> i64 { if a > b { return 1 } return 0 } 19func dg_not(v: i64) -> i64 { if v == 0 { return 1 } return 0 } 20func dg_show(label: *u8, v: i64) -> i64 { gv_puts(label); gv_num(v); gv_puts("\n" as *u8); return 0 } 21// does the span txt[off..off+len) equal the whole null-terminated expected string? 22func dg_spaneq(txt: *u8, off: i64, len: i64, exp: *u8) -> i64 { 23 var i: i64 = 0 24 while i < len { if txt[off + i] != exp[i] { return 0 } i = i + 1 } 25 if exp[len] != (0 as u8) { return 0 } 26 return 1 27} 28 29func main(argc: i64, argv: *i64) -> i64 { 30 gv_head("=== nx_docprose gate -- stored-document front matter (fixtures = the live 2026-08-25 defect) ===" as *u8) 31 let ctr: *i64 = gv_ctr() 32 let bo: *i64 = sys_mmap(16) as *i64 33 let bl: *i64 = sys_mmap(16) as *i64 34 let bx: *i64 = sys_mmap(16) as *i64 35 36 // ---- FIXTURE 1: a raw HTTP wire capture, the shape actually found in the index. 37 let cap1: *u8 = "HTTP/1.1 200 OK\nDate: Thu, 02 Jul 2026 17:07:07 GMT\nContent-Type: text/html\n\nSerper - The Worlds Fastest Search API\nWe use cookies to improve.\n" as *u8 38 let c1n: i64 = dg_len(cap1) 39 40 // ANTI-VACUITY: prove the OLD rule would have failed here. The first non-empty line of this fixture 41 // really is the status line, so "first non-empty line" really would serve it as the title. 42 gv_check("fixture-reached-first-line-IS-an-http-status-line" as *u8, dpr_is_header_line(cap1, c1n, 0, dpr_line_end(cap1, c1n, 0)), ctr) 43 44 let b1: i64 = dpr_http_body(cap1, c1n) 45 dg_show(" dpr_http_body -> " as *u8, b1) 46 gv_check("http-body-offset-is-found" as *u8, dg_gt(b1, 0), ctr) 47 gv_check("http-body-offset-lands-on-the-payload-S" as *u8, dg_eq(cap1[b1] as i64, 83), ctr) 48 49 gv_check("title-of-a-capture-is-the-PAGE-title-not-the-status-line" as *u8, 50 dpr_title(cap1, c1n, DG_CAP, bo, bl, bx), ctr) 51 gv_check("title-text-is-exactly-the-page-title" as *u8, 52 dg_spaneq(cap1, bo[0], bl[0], "Serper - The Worlds Fastest Search API" as *u8), ctr) 53 gv_check("announce-reports-the-http-bytes-it-stepped-over" as *u8, dg_eq(dpr_last_http_skipped(), b1), ctr) 54 55 // ---- NEG-CONTROL: ANCHORED, NOT A SCAN. A document that merely DISCUSSES HTTP must be left alone; 56 // a heuristic that searched for a status line anywhere would mutilate this one. 57 let disc: *u8 = "Understanding the protocol in practice\nHTTP/1.1 200 OK is a status line.\n" as *u8 58 let dn: i64 = dg_len(disc) 59 gv_check("neg-control-prose-that-MENTIONS-http-is-not-treated-as-a-capture" as *u8, dg_eq(dpr_http_body(disc, dn), 0), ctr) 60 dpr_title(disc, dn, DG_CAP, bo, bl, bx) 61 gv_check("neg-control-that-prose-keeps-its-own-first-line-as-title" as *u8, 62 dg_spaneq(disc, bo[0], bl[0], "Understanding the protocol in practice" as *u8), ctr) 63 64 // ---- FIXTURE 2: raw markup leading the stored text (the live "<p align=...>" title). 65 let mk: *u8 = "<p align=x>\n<div class=nav>\nReal Article Title Here\nbody text follows\n" as *u8 66 let mn: i64 = dg_len(mk) 67 gv_check("fixture-reached-first-line-IS-markup-only" as *u8, dpr_is_markup_line(mk, mn, 0, dpr_line_end(mk, mn, 0)), ctr) 68 dpr_title(mk, mn, DG_CAP, bo, bl, bx) 69 gv_check("markup-only-leading-lines-are-skipped" as *u8, 70 dg_spaneq(mk, bo[0], bl[0], "Real Article Title Here" as *u8), ctr) 71 gv_check("announce-counts-the-two-markup-lines-skipped" as *u8, dg_eq(dpr_last_lines_skipped(), 2), ctr) 72 73 // ---- NEG-CONTROL: a clean document must be UNTOUCHED. If this fails the skipper is eating content. 74 let clean: *u8 = "Estate Planning Basics\nwills living trusts probate\n" as *u8 75 let cn: i64 = dg_len(clean) 76 gv_check("neg-control-clean-doc-content-starts-at-byte-zero" as *u8, dg_eq(dpr_content_start(clean, cn), 0), ctr) 77 dpr_title(clean, cn, DG_CAP, bo, bl, bx) 78 gv_check("neg-control-clean-doc-keeps-its-real-title" as *u8, 79 dg_spaneq(clean, bo[0], bl[0], "Estate Planning Basics" as *u8), ctr) 80 gv_check("neg-control-clean-doc-skips-NO-lines" as *u8, dg_eq(dpr_last_lines_skipped(), 0), ctr) 81 82 // ---- a line with no WORD in it is not a title (a rule of punctuation, not of taste) 83 let pun: *u8 = "---\n===\nActual Title After Rules\n" as *u8 84 let pn: i64 = dg_len(pun) 85 dpr_title(pun, pn, DG_CAP, bo, bl, bx) 86 gv_check("punctuation-only-lines-are-not-served-as-titles" as *u8, 87 dg_spaneq(pun, bo[0], bl[0], "Actual Title After Rules" as *u8), ctr) 88 89 // ---- COMPOSITION: the title must come back cut on a TOKEN boundary, because dpr_title delegates to 90 // nx_textcut. A long single line under the live 72-byte cap is the exact production case. 91 let lng: *u8 = "NIST Reference Fluid Thermodynamic and Transport Properties Database (REFPROP) Version 9\n" as *u8 92 let ln: i64 = dg_len(lng) 93 dpr_title(lng, ln, DG_CAP, bo, bl, bx) 94 dg_show(" long-title len -> " as *u8, bl[0]) 95 gv_check("long-title-is-cut-within-the-cap" as *u8, dg_eq(bl[0] <= DG_CAP, 1), ctr) 96 gv_check("long-title-does-not-end-mid-word" as *u8, tc_is_wordch(lng[bo[0] + bl[0] - 1] as i64), ctr) 97 gv_check("long-title-ends-with-the-word-Database" as *u8, 98 dg_spaneq(lng, bo[0], bl[0], "NIST Reference Fluid Thermodynamic and Transport Properties Database" as *u8), ctr) 99 100 // ---- HONEST FAILURE: a document with no titled line at all returns 0 rather than inventing one. 101 let none: *u8 = "<a>\n<b>\n" as *u8 102 gv_check("no-titled-line-returns-an-honest-zero" as *u8, dg_not(dpr_title(none, dg_len(none), DG_CAP, bo, bl, bx)), ctr) 103 104 // ---- THE DEGRADED PATH: a capture that lost its header LINE STRUCTURE before storage. Measured live 105 // 2026-08-25 -- indexed docs whose headers are space-separated on ONE line, so there is no blank line. 106 // THIS FIXTURE IS THE DISCRIMINATING ONE: it carries a Link header holding "<https://...>" BEFORE the 107 // doctype. A bare-'<' anchor would land inside that header and confidently report the wrong body 108 // offset; only the <!doctype anchor is correct. If this tooth ever passes with a bare-'<' rule, the 109 // fixture has stopped discriminating. 110 let coll: *u8 = "HTTP/1.1 200 OK Date: Thu, 02 Jul 2026 16:53:48 GMT Link: <https://example.com/x>; rel=canonical Content-Type: text/html; charset=utf-8 <!doctype html><title>Real Page Title</title>" as *u8 111 let coln: i64 = dg_len(coll) 112 gv_check("fixture-reached-collapsed-capture-has-NO-blank-line" as *u8, dg_eq(dpr_find_ci(coll, 0, coln, "\n\n" as *u8), 0 - 1), ctr) 113 let cb: i64 = dpr_http_body(coll, coln) 114 dg_show(" degraded dpr_http_body -> " as *u8, cb) 115 gv_check("collapsed-html-capture-body-IS-located" as *u8, dg_gt(cb, 0), ctr) 116 gv_check("degraded-anchor-lands-on-a-tag-open" as *u8, dg_eq(coll[cb] as i64, DG_CH_LT), ctr) 117 gv_check("degraded-anchor-lands-on-DOCTYPE-not-the-Link-header-angle-bracket" as *u8, dg_eq(coll[cb + 1] as i64, DG_CH_BANG), ctr) 118 gv_check("degraded-path-ANNOUNCES-itself-as-degraded" as *u8, dg_eq(dpr_last_degraded(), 1), ctr) 119 120 // NEG-CONTROL: a collapsed capture of a NON-html declared type has nothing sound to anchor on and must 121 // return an honest zero rather than reaching for the markup anyway. 122 let colp: *u8 = "HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 <!doctype html><title>Nope</title>" as *u8 123 gv_check("neg-control-collapsed-NON-html-capture-declines" as *u8, dg_eq(dpr_http_body(colp, dg_len(colp)), 0), ctr) 124 125 // NEG-CONTROL: the EXACT path must still win where it applies, and must NOT be reported as degraded -- 126 // otherwise every caller reading the announce would treat a sound answer as a weak one. 127 dpr_http_body(cap1, c1n) 128 gv_check("neg-control-exact-path-is-NOT-flagged-degraded" as *u8, dg_eq(dpr_last_degraded(), 0), ctr) 129 130 return gv_verdict("nx_docprose_gate" as *u8, ctr, "front-matter skipping proven on the measured live captures, with neg-controls against over-skipping" as *u8) 131}