code wiki / _hdl_build / nx_tpl_parts_gate.nx
nx_tpl_parts_gate.nx source
↩ module page · 97 lines · 4999 B
1// nx_tpl_parts_gate.nx -- referee for R-PARTS (nx_tpl_parts). Proves the LEGO half of the ingest: section-level
2// bricks extracted, content-addressed + deduped (near-dup template contributes 0 new bricks), idempotent across
3// runs, budget-guarded (fail-fast), junk skipped, and a recomputed brick key round-trips through the store with
4// a faithful record. GREEN iff 7/7. Wrapper pre-cleans /tmp/partcorpus_gate and /tmp/partcorpus_gate2.
5// license_tier: ORIGINAL expect_exit: 0
6import "nx_gate.nx"
7import "nx_tpl_parts.nx"
8
9func pg_row(name: *u8, ok: i64) -> i64 { gw(" " as *u8); gw(name); if ok==1 { gw(" PASS\n" as *u8) } else { gw(" FAIL\n" as *u8) } return 0 }
10func pg_has(hay: *u8, hn: i64, needle: *u8) -> i64 {
11 if (hay as i64)==0 { return 0 }
12 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
13 if nl==0 { return 1 }
14 var i: i64=0
15 while i+nl<=hn { var j: i64=0; var ok: i64=1; while j<nl { if (hay[i+j] as i64)!=(needle[j] as i64) { ok=0; j=nl } else { j=j+1 } } if ok==1 { return 1 } i=i+1 }
16 return 0
17}
18
19func main() -> i64 {
20 // T1/T3: first run over the seed dir (landing + landing2 near-dup + shop + resume + junk)
21 let c1: *i64 = sys_mmap(64) as *i64
22 tp_run("web_assets/_tpl_seed" as *u8, "/tmp/partcorpus_gate/" as *u8, 2000, c1)
23 var t1: i64=0; if c1[0] >= 6 { t1=1 } // bricks stored
24 var t3: i64=0; if c1[1] >= 4 { t3=1 } // near-dup landing2 collapsed into dups
25 var t7: i64=0; if c1[2] >= 1 { t7=1 } // junk file skipped (no markup)
26 // T2: re-run is idempotent (cross-run dedup)
27 let c2: *i64 = sys_mmap(64) as *i64
28 tp_run("web_assets/_tpl_seed" as *u8, "/tmp/partcorpus_gate/" as *u8, 2000, c2)
29 var t2: i64=0; if c2[0] == 0 { if c2[1] >= c1[0] { t2=1 } }
30 // T4 (NEG): budget trap fires fail-fast on a fresh store
31 let c3: *i64 = sys_mmap(64) as *i64
32 tp_run("web_assets/_tpl_seed" as *u8, "/tmp/partcorpus_gate2/" as *u8, 2, c3)
33 var t4: i64=0; if c3[5] == 1 { if c3[0] <= 2 { t4=1 } }
34 // T5/T6: recompute the landing hero brick's key EXACTLY as the organ does -> present in store + faithful record
35 let szp: *i64 = sys_mmap(16) as *i64
36 let land: *u8 = sys_read_file("web_assets/_tpl_seed/landing.html" as *u8, szp)
37 let ln: i64 = szp[0]
38 let toks: *i64 = sys_mmap(TP_MAXK * 8) as *i64
39 let offs: *i64 = sys_mmap(TP_MAXK * 8) as *i64
40 let mets: *i64 = sys_mmap(8 * 8) as *i64
41 let key: *u8 = sys_mmap(64)
42 let np: i64 = tp_scan(land, ln, toks, offs)
43 var t5: i64=0
44 var t6: i64=0
45 if np >= 4 {
46 // find the hero token among the scanned parts
47 var hi: i64 = 0 - 1
48 var i: i64 = 0
49 while i < np {
50 let tk: *u8 = toks[i] as *u8
51 if tk[0] == (104 as u8) { if tk[1] == (101 as u8) { hi = i; i = np } }
52 i = i + 1
53 }
54 if hi >= 0 {
55 var to2: i64 = ln
56 if hi + 1 < np { to2 = offs[hi + 1] }
57 tp_metrics(land, offs[hi], to2, mets)
58 tp_mkkey("hero" as *u8, mets, key)
59 let pbox: *i64 = sys_mmap(16) as *i64
60 let lbox: *i64 = sys_mmap(16) as *i64
61 let hh: *i64 = ss_open("/tmp/partcorpus_gate/" as *u8)
62 if (hh as i64) != 0 {
63 if ss_hget(hh, key, pbox, lbox) == 1 {
64 t5 = 1
65 let rp: *u8 = pbox[0] as *u8
66 let rl: i64 = lbox[0]
67 if pg_has(rp, rl, "kind|hero" as *u8) == 1 { if pg_has(rp, rl, "src|" as *u8) == 1 { t6 = 1 } }
68 }
69 }
70 }
71 }
72
73 var passes: i64=0
74 if t1==1 { passes=passes+1 } if t2==1 { passes=passes+1 } if t3==1 { passes=passes+1 }
75 if t4==1 { passes=passes+1 } if t5==1 { passes=passes+1 } if t6==1 { passes=passes+1 } if t7==1 { passes=passes+1 }
76 var green: i64=0; if passes==7 { green=1 }
77
78 gw("=== nx_tpl_parts_gate -- R-PARTS section-brick (lego) ingest ===\n" as *u8)
79 pg_row("T1-bricks-stored " as *u8, t1)
80 pg_row("T2-idempotent-rerun " as *u8, t2)
81 pg_row("T3-neardup-collapse(neg) " as *u8, t3)
82 pg_row("T4-budget-trap-firefast(neg) " as *u8, t4)
83 pg_row("T5-recomputed-key-roundtrip " as *u8, t5)
84 pg_row("T6-record-faithful " as *u8, t6)
85 pg_row("T7-junk-skipped(neg) " as *u8, t7)
86 gw("verdict=" as *u8); if green==1 { gw("GREEN" as *u8) } else { gw("RED" as *u8) }
87 gw(" passes=" as *u8); gn(passes); gw("/7 stored=" as *u8); gn(c1[0]); gw(" dups=" as *u8); gn(c1[1]); gw(" parts_seen=" as *u8); gn(c1[7]); gw("\n" as *u8)
88
89 let lg: i64 = sys_openat_append("knowledge/status/tpl_parts.log" as *u8, 0x1a4)
90 if lg >= 0 {
91 if green==1 { let m: *u8 = "TPL-PARTS rows=7 pass=7 verdict=GREEN\n" as *u8; sys_write(lg, m, 38) }
92 else { let m2: *u8 = "TPL-PARTS verdict=RED\n" as *u8; sys_write(lg, m2, 22) }
93 sys_close(lg)
94 }
95 if green==1 { return 0 }
96 return 1
97}