code wiki / _hdl_build / nx_research_engine_admission_t68_gate.nx
nx_research_engine_admission_t68_gate.nx source
↩ module page · 127 lines · 8247 B
1// nx_research_engine_gate.nx -- s-class gate for the shared research engine (nx_research_engine). Proves the
2// EXTRACT (rf_strip) and the GZIP-guard (rf_is_gzip) DETERMINISTICALLY, no network -- the functionality the engine
3// guarantees, measured + repeatable. This is the functionality the old copy-pasted fetchers carried, now
4// CENTRALIZED into one DRY engine and tested ONCE (rule 15). No functionality reduced; gzip-guard + extract proven.
5// (helpers carry a ge_ prefix -- the engine pulls in the TLS stack, so generic names like streq/slen/chk collide.)
6// criteria:
7// 1 rf_strip drops tags, keeps text: "<p>hi <b>there</b></p>" -> "hi there "
8// 2 rf_strip SUPPRESSES <script> body: "<script>var x=1;</script>OK" -> "OK"
9// 3 rf_strip collapses whitespace: "a b" -> "a b"
10// 4 rf_strip handles <style> + blocks: "<div>A</div><style>x{}</style><p>B</p>" -> "A B "
11// 5 rf_is_gzip: 0x1f 0x8b body detected (would skip), plain text NOT (would bank)
12// expect_exit: 0 license_tier: ORIGINAL
13import "nx_research_engine_admission_t68.nx"
14import "nx_gate_verdict.nx"
15
16func ge_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
17func ge_streq(a: *u8, an: i64, b: *u8) -> i64 {
18 var i: i64 = 0
19 while i < an { if (b[i] as i64) == 0 { return 0 } if a[i] != b[i] { return 0 } i = i + 1 }
20 if (b[an] as i64) != 0 { return 0 }
21 return 1
22}
23func ge_chk(name: *u8, ok: i64) -> i64 {
24 if ok == 1 { rf_puts(" PASS " as *u8); rf_puts(name); rf_puts("\n" as *u8); return 1 }
25 rf_puts(" FAIL " as *u8); rf_puts(name); rf_puts("\n" as *u8); return 0
26}
27func ge_tstrip(h: *u8, expect: *u8) -> i64 {
28 let o: *u8 = sys_mmap(4096)
29 let n: i64 = rf_strip(h, ge_slen(h), o, 4096)
30 return ge_streq(o, n, expect)
31}
32
33func main() -> i64 {
34 rf_puts("=== nx_research_engine gate -- shared fetch+extract+bank engine: functionality CENTRALIZED + tested ===\n" as *u8)
35 var pass: i64 = 0
36 var total: i64 = 0
37 let r1: i64 = ge_tstrip("<p>hi <b>there</b></p>" as *u8, "hi there " as *u8)
38 total = total + 1; pass = pass + ge_chk("T1 drop tags -> 'hi there '" as *u8, r1)
39 let r2: i64 = ge_tstrip("<script>var x=1;</script>OK" as *u8, "OK" as *u8)
40 total = total + 1; pass = pass + ge_chk("T2 suppress <script> body -> 'OK'" as *u8, r2)
41 let r3: i64 = ge_tstrip("a b" as *u8, "a b" as *u8)
42 total = total + 1; pass = pass + ge_chk("T3 collapse whitespace -> 'a b'" as *u8, r3)
43 let r4: i64 = ge_tstrip("<div>A</div><style>x{}</style><p>B</p>" as *u8, "A B " as *u8)
44 total = total + 1; pass = pass + ge_chk("T4 <style> + multi-block -> 'A B '" as *u8, r4)
45 let gz: *u8 = sys_mmap(8); gz[0] = 0x1f as u8; gz[1] = 0x8b as u8; gz[2] = 0 as u8
46 var t5: i64 = 0
47 if rf_is_gzip(gz, 3) == 1 { if rf_is_gzip("hello" as *u8, 5) == 0 { t5 = 1 } }
48 total = total + 1; pass = pass + ge_chk("T5 gzip guard: 0x1f8b skipped, text banked" as *u8, t5)
49
50
51 let fixture: *u8 = sys_mmap(512)
52 let pidbox: *u8 = sys_mmap(64)
53 let stamp: i64 = sys_now_realtime_us()
54 var pos: i64 = rf_scat(fixture, 0, "/tmp/research-admission-t68-" as *u8)
55 // Timestamp gives this retained fixture an independent namespace on each run.
56 let tempfd: i64 = 0
57 let digits: *u8 = sys_mmap(64)
58 var v: i64 = stamp; var dn: i64 = 0
59 while v > 0 { digits[dn] = (48 + (v % 10)) as u8; dn = dn + 1; v = v / 10 }
60 while dn > 0 { dn = dn - 1; fixture[pos] = digits[dn]; pos = pos + 1 }
61 fixture[pos] = 0 as u8
62 if sys_mkdir(fixture, 0x1ed) != 0 { return 91 }
63 if sys_chdir(fixture) != 0 { return 92 }
64 rf_puts("fixture=" as *u8); rf_puts(fixture); rf_puts("\n" as *u8)
65 let f: i64 = rf_slot_lock("owned.lk" as *u8)
66 total = total + 1; pass = pass + ge_chk("new owned lease" as *u8, (f >= 0) as i64)
67 if f < 0 { return 93 }
68 let contender: i64 = rf_slot_lock("owned.lk" as *u8)
69 total = total + 1; pass = pass + ge_chk("separate open is contended" as *u8, (contender == RF_SLOT_BUSY) as i64)
70 total = total + 1; pass = pass + ge_chk("legacy reader finds zero digits" as *u8, (rf_rint("owned.lk" as *u8) == 0) as i64)
71 let child: i64 = sys_fork()
72 if child == 0 {
73 sys_close(f)
74 let cf: i64 = rf_slot_lock("owned.lk" as *u8)
75 if cf == RF_SLOT_BUSY { sys_exit(0) }
76 if cf >= 0 { sys_close(cf) }
77 sys_exit(1)
78 }
79 let ws: *i64 = sys_mmap(16) as *i64
80 var cross: i64 = 0
81 if child > 0 { if sys_wait4(child, ws, 0) == child { if wait_exit_code(ws[0]) == 0 { cross = 1 } } }
82 total = total + 1; pass = pass + ge_chk("child separate open cannot steal parent lease" as *u8, cross)
83 rf_slot_release("." as *u8, 0, f)
84 let reopened: i64 = rf_slot_lock("owned.lk" as *u8)
85 total = total + 1; pass = pass + ge_chk("close releases persistent inode" as *u8, (reopened >= 0) as i64)
86 if reopened >= 0 { sys_close(reopened) }
87 let crash: i64 = sys_fork()
88 if crash == 0 { let deadfd: i64 = rf_slot_lock("crash.lk" as *u8); if deadfd < 0 { sys_exit(1) } sys_exit(0) }
89 var deadok: i64 = 0
90 if crash > 0 { if sys_wait4(crash, ws, 0) == crash { if wait_exit_code(ws[0]) == 0 { let after: i64 = rf_slot_lock("crash.lk" as *u8); if after >= 0 { deadok = 1; sys_close(after) } } } }
91 total = total + 1; pass = pass + ge_chk("process exit releases unclosed lease" as *u8, deadok)
92 let old: i64 = sys_openat_exclusive("legacy.lk" as *u8, RF_MODE)
93 sys_write(old, "1\n" as *u8, 2)
94 let legacy: i64 = rf_slot_lock("legacy.lk" as *u8)
95 total = total + 1; pass = pass + ge_chk("ancient live legacy record not age stolen" as *u8, (legacy == RF_SLOT_LEGACY) as i64)
96 total = total + 1; pass = pass + ge_chk("legacy bytes retained" as *u8, (rf_rint("legacy.lk" as *u8) == 1) as i64)
97 sys_close(old)
98 let empty: i64 = sys_openat_exclusive("empty.lk" as *u8, RF_MODE)
99 total = total + 1; pass = pass + ge_chk("create before initialization refuses" as *u8, (rf_slot_lock("empty.lk" as *u8) == RF_SLOT_LEGACY) as i64)
100 sys_close(empty)
101 rf_save("bad.lk" as *u8, "NXRFLOCK\nX" as *u8, 10)
102 total = total + 1; pass = pass + ge_chk("trailing marker bytes refused" as *u8, (rf_slot_lock("bad.lk" as *u8) == RF_SLOT_LEGACY) as i64)
103 sys_symlinkat("owned.lk" as *u8, "link.lk" as *u8)
104 total = total + 1; pass = pass + ge_chk("final symlink refused" as *u8, (rf_slot_lock("link.lk" as *u8) < 0) as i64)
105 let idx: *i64 = sys_mmap(16) as *i64
106 total = total + 1; pass = pass + ge_chk("missing directory refuses promptly" as *u8, (rf_slot_acquire("absent" as *u8, 1, idx) < 0) as i64)
107 total = total + 1; pass = pass + ge_chk("invalid slot capacity refused" as *u8, (rf_slot_acquire("." as *u8, 5, idx) == (0 - 22)) as i64)
108 sys_mkdir("knowledge" as *u8, 0x1ed); sys_mkdir("knowledge/fetched" as *u8, 0x1ed); sys_mkdir("knowledge/library" as *u8, 0x1ed)
109 // A regular file obstructs the real admission directory. No source URL is supplied.
110 rf_save("knowledge/status" as *u8, "fixture" as *u8, 7)
111 let out: *u8 = sys_mmap(4096); out[0] = 77 as u8
112 let bank: i64 = rf_fetch_bank("" as *u8, "refused" as *u8, 0 as *TrustStore, out, 4096)
113 var refused: i64 = 0
114 if bank == 0 { if out[0] == (77 as u8) { if rf_have("knowledge/library/refused.fail" as *u8) == 0 { if rf_have("knowledge/fetched/refused.raw" as *u8) == 0 { if rf_have("knowledge/library/refused.txt" as *u8) == 0 { refused = 1 } } } } }
115 total = total + 1; pass = pass + ge_chk("actual bank refusal leaves output and fetch sidecars untouched" as *u8, refused)
116
117 rf_puts("NX-RESEARCH-ENGINE-GATE " as *u8); rf_putn(pass); rf_puts(" / " as *u8); rf_putn(total)
118 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
119 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
120 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
121 let ctr__dry: *i64 = gv_ctr()
122 ctr__dry[0] = pass
123 ctr__dry[1] = total
124 let rc__dry: i64 = gv_verdict("RESEARCH-ENGINE-GATE" as *u8, ctr__dry, "extract + gzip-guard proven; ONE DRY engine, structured topic organs, no functionality lost)" as *u8)
125 sys_exit(rc__dry)
126 return rc__dry
127}