code wiki / _hdl_build / nx_outlink_harvest_gate.nx

nx_outlink_harvest_gate.nx source

↩ module page · 74 lines · 5314 B

1import "nx_gate_gn.nx" 2// nx_outlink_harvest_gate.nx — gate for the shared outlink harvester (P1 PageRank feed). license_tier: ORIGINAL 3// Proves: (T1) olh_scan finds absolute + root-relative <a href> links and rejects assets/fragments; (T2) each 4// edge cid is BYTE-IDENTICAL to ci_hash(resolved_url) — the property that makes an outlink resolve to a real 5// graph node (== dss_urlcid at serve time); (T3) olh_urlok gates content vs junk; (T4) olh_outkey key shape. 6import "nx_outlink_harvest.nx" // olh_scan / olh_urlok / olh_outkey / olh_len ; ci_hash via nx_corpus_ingest 7 8func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } __syscall(1, 1, s, n); return 0 } 9func g_streq(a: *u8, b: *u8) -> i64 { 10 var i: i64 = 0 11 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 12 if b[i] != (0 as u8) { return 0 } 13 return 1 14} 15 16func main() -> i64 { 17 var pass: i64 = 0 18 var fail: i64 = 0 19 20 // --- T1: scan a page with 5 hrefs (abs, root-relative UPPERCASE, en.wikipedia OK, .png, fragment) --- 21 // expect exactly 3 edges: the .png and the #fragment are rejected by olh_urlok. 22 let base: *u8 = "https://example.org/dir/start" as *u8 23 let html: *u8 = "<p>hi</p><a href='https://example.com/page'>x</a> j <A HREF='/about'>y</a> z <a href='https://en.wikipedia.org/wiki/Foo'>w</a> <a href='https://bad.com/pic.png'>i</a> <a href='https://bad.com/q#f'>f</a>" as *u8 24 let eb: *i64 = sys_mmap(64 * 8) as *i64 25 let n: i64 = olh_scan(html, olh_len(html), base, olh_len(base), eb, 64) 26 if n == 3 { pass = pass + 1 } else { fail = fail + 1; gw("T1 FAIL nedge=" as *u8); gn(n); gw(" (want 3)\n" as *u8) } 27 28 // --- T2: each edge cid == ci_hash(EXACT resolved url) — the node-identity contract --- 29 let u0: *u8 = "https://example.com/page" as *u8 30 let u1: *u8 = "https://example.org/about" as *u8 // root-relative resolved HOST-based (not base+path) 31 let u2: *u8 = "https://en.wikipedia.org/wiki/Foo" as *u8 32 if eb[0] == ci_hash(u0, olh_len(u0)) { pass = pass + 1 } else { fail = fail + 1; gw("T2a FAIL abs-link cid mismatch\n" as *u8) } 33 if eb[1] == ci_hash(u1, olh_len(u1)) { pass = pass + 1 } else { fail = fail + 1; gw("T2b FAIL root-rel got=" as *u8); gn(eb[1]); gw(" want=" as *u8); gn(ci_hash(u1, olh_len(u1))); gw("\n" as *u8) } 34 if eb[2] == ci_hash(u2, olh_len(u2)) { pass = pass + 1 } else { fail = fail + 1; gw("T2c FAIL wiki-link cid mismatch\n" as *u8) } 35 // and all three edge cids are non-negative + distinct (real node ids) 36 if eb[0] >= 0 { if eb[1] >= 0 { if eb[2] >= 0 { pass = pass + 1 } else { fail = fail + 1 } } else { fail = fail + 1 } } else { fail = fail + 1 } 37 if eb[0] != eb[1] { if eb[0] != eb[2] { if eb[1] != eb[2] { pass = pass + 1 } else { fail = fail + 1; gw("T2e dup\n" as *u8) } } else { fail = fail + 1; gw("T2e dup\n" as *u8) } } else { fail = fail + 1; gw("T2e dup\n" as *u8) } 38 39 // --- T3: olh_urlok content filter --- 40 let r1: *u8 = "https://a.com/real-page" as *u8 41 let r2: *u8 = "https://a.com/image.png" as *u8 42 let r3: *u8 = "ftp://a.com/x" as *u8 43 let r4: *u8 = "https://a.com/x?query=1" as *u8 44 let r5: *u8 = "https://de.wikipedia.org/wiki/X" as *u8 // non-en wikipedia -> reject 45 if olh_urlok(r1, olh_len(r1)) == 1 { pass = pass + 1 } else { fail = fail + 1; gw("T3a\n" as *u8) } 46 if olh_urlok(r2, olh_len(r2)) == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T3b\n" as *u8) } 47 if olh_urlok(r3, olh_len(r3)) == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T3c\n" as *u8) } 48 if olh_urlok(r4, olh_len(r4)) == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T3d\n" as *u8) } 49 if olh_urlok(r5, olh_len(r5)) == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T3e\n" as *u8) } 50 51 // --- T4: olh_outkey builds "out:<decimal cid>" --- 52 let okey: *u8 = sys_mmap(64) 53 olh_outkey(12345, okey) 54 if g_streq(okey, "out:12345" as *u8) == 1 { pass = pass + 1 } else { fail = fail + 1; gw("T4 FAIL key=" as *u8); gw(okey); gw("\n" as *u8) } 55 56 // --- T6: olh_resolve_root — host-based, byte-checked (the anti-phantom-node contract) --- 57 let rr: *u8 = sys_mmap(8192) 58 let hrefa: *u8 = "/about" as *u8 59 let rl6: i64 = olh_resolve_root("https://example.org/dir/start" as *u8, hrefa, olh_len(hrefa), rr) 60 var t6a: i64 = 0 61 if rl6 == 25 { if g_streq(rr, "https://example.org/about" as *u8) == 1 { t6a = 1 } } 62 if t6a == 1 { pass = pass + 1 } else { fail = fail + 1; gw("T6a FAIL rl=" as *u8); gn(rl6); gw(" got=" as *u8); gw(rr); gw("\n" as *u8) } 63 if olh_resolve_root("no-scheme-base" as *u8, hrefa, olh_len(hrefa), rr) == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T6b FAIL\n" as *u8) } 64 65 // --- T5: empty / no-link html -> zero edges (no spurious rows) --- 66 let h2: *u8 = "<p>no links here at all</p>" as *u8 67 let eb2: *i64 = sys_mmap(8 * 8) as *i64 68 let n2: i64 = olh_scan(h2, olh_len(h2), base, olh_len(base), eb2, 8) 69 if n2 == 0 { pass = pass + 1 } else { fail = fail + 1; gw("T5 FAIL nedge=" as *u8); gn(n2); gw("\n" as *u8) } 70 71 gw("outlink-harvest gate: " as *u8); gn(pass); gw(" pass / " as *u8); gn(fail); gw(" fail\n" as *u8) 72 if fail == 0 { gw("OUTLINK-HARVEST GATE GREEN\n" as *u8); return 0 } 73 gw("OUTLINK-HARVEST GATE RED\n" as *u8); return 1 74}