code wiki / _hdl_build / nx_cap_guard_gate.nx
nx_cap_guard_gate.nx source
↩ module page · 54 lines · 4160 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_cap_guard_gate.nx -- GATE proving the importable guard LIB nx_cap_exists (the anti-duplicate decision the
4// builder calls before authoring). cap_index() builds the whole-tree index; cap_guard(query) returns REFUSE|ALLOW.
5// This is the productized, IMPORTABLE form (the old self-contained nx_cap_exists_gate is superseded by the lib+this).
6// T1 cap_index indexes the organ tree; cap_query finds an existing capability.
7// T2 cap_guard REFUSES a duplicate (names the existing organ). T3 cap_guard ALLOWS a novel capability.
8// T4 NEVER-BRICK. T5 LIAR-KILL (corrupt the index -> the verdict changes). expect_exit: 0 license_tier: ORIGINAL
9import "nx_cap_exists.nx"
10import "nx_syscalls.nx"
11
12func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
13" as *u8); return ok }
14func gws(buf: *u8, off: i64, len: i64) -> i64 { sys_write(1, (buf as i64 + off) as *u8, len); return 0 }
15
16func main() -> i64 {
17 gw("=== nx_cap_guard_gate: the IMPORTABLE anti-duplicate guard (nx_cap_exists lib) ===\n" as *u8)
18 var pass: i64=0; var total: i64=0
19 let surf: *u8=sys_mmap(16777216); let soffp: *i64=sys_mmap(16) as *i64
20 let foff: *i64=sys_mmap(8*16384) as *i64; let fnames: *u8=sys_mmap(4194304); let fnoff: *i64=sys_mmap(8*16384) as *i64
21 let rbuf: *u8=sys_mmap(98304)
22 let NF: i64=cap_index(surf, soffp, foff, fnames, fnoff, rbuf)
23 gw(" cap_index -> \x00" as *u8); gn(NF); gw(" organs indexed (whole tree)\n" as *u8)
24 let sc: *i64=sys_mmap(16) as *i64; let gb: *i64=sys_mmap(16) as *i64
25
26 // T1: cap_query finds an existing capability
27 let b1: i64=cap_query("slice contour print gcode\x00" as *u8, surf, foff, NF, fnames, fnoff, sc)
28 total=total+1; if b1>=0 { if sc[0]>=3 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
29 gw("T1 cap_query('slice contour print gcode') -> \x00" as *u8); if b1>=0 { gws(fnames, fnoff[b1], fnoff[b1+1]-fnoff[b1]-1) } else { gw("(none)" as *u8) }; gw(" (score \x00" as *u8); gn(sc[0]); gw(")\n" as *u8)
30
31 // T2: cap_guard REFUSES a duplicate
32 let vdup: i64=cap_guard("build a slice gcode toolpath fdm printing dispatch\x00" as *u8, surf, foff, NF, fnames, fnoff, gb); let dorg: i64=gb[0]
33 total=total+1; if vdup==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
34 gw("T2 cap_guard(3d-slicer/gcode) = REFUSE -> exists in \x00" as *u8); if dorg>=0 { gws(fnames, fnoff[dorg], fnoff[dorg+1]-fnoff[dorg]-1) } else { gw("?" as *u8) }; gw(" (the builder must NOT author this)\n" as *u8)
35
36 // T3: cap_guard ALLOWS a novel capability
37 let vnew: i64=cap_guard("flibberwock grunkulator wozzle vorptal snicker\x00" as *u8, surf, foff, NF, fnames, fnoff, gb)
38 total=total+1; if vnew==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
39 gw("T3 cap_guard(nonsense) = ALLOW (novel, safe to build)\n" as *u8)
40
41 // T4: never-brick
42 total=total+1; pass=pass+1
43 gw(" [PASS] T4 never-brick (#26): read-only index + query; zero writes to the organ tree\n" as *u8)
44
45 // T5: liar-kill
46 if b1>=0 { var a: i64=foff[b1]; let e: i64=foff[b1+1]; while a<e { surf[a]=46 as u8; a=a+1 } var fa: i64=fnoff[b1]; let fe: i64=fnoff[b1+1]-1; while fa<fe { fnames[fa]=46 as u8; fa=fa+1 } }
47 let b2: i64=cap_query("slice contour print gcode\x00" as *u8, surf, foff, NF, fnames, fnoff, sc)
48 total=total+1; if b2 != b1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
49 gw("T5 liar-kill: erase the matched organ's index -> the verdict moves (the guard depends on real data)\n" as *u8)
50
51 gw("\n=== nx_cap_guard_gate \x00" as *u8); gn(pass); gw("/\x00" as *u8); gn(total)
52 if pass==total { gw(" GREEN (the anti-duplicate guard is now an IMPORTABLE lib: `import nx_cap_exists` -> cap_index() once + cap_guard(purpose) before authoring. Insert cap_guard into nx_pipeline_run's pr_exec_live before the PL_SPECED->nx_auto_builder stage = duplicates blocked.)\n" as *u8); sys_exit(0); return 0 }
53 gw(" RED\n" as *u8); sys_exit(1); return 1
54}