code wiki / _hdl_build / nx_ad_emitter_registry_gate.nx

nx_ad_emitter_registry_gate.nx source

↩ module page · 85 lines · 6067 B

1// nx_ad_emitter_registry_gate.nx -- GATE for the builder's learned emitter catalog. Hermetic (/tmp registry): 2// teach -> round-trip, idempotent re-teach (no dup), GROW (count increases), and the SAFETY invariant -- a 3// template carrying a <script or src= is REJECTED (the growing library stays 0-third-party-JS / 0-fetch by 4// construction; the detector is proven to FIRE so a pass means proven-safe). Sovereign. license_tier: ORIGINAL 5import "nx_ad_emitter_registry.nx" 6import "nx_syscalls.nx" 7import "nx_gate_verdict.nx" 8 9const GT_PATH: *u8 = "/tmp/ad_emitters_gate.tsv" 10 11func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 12func g_i(v: i64) -> i64 { let t: *u8 = sys_mmap(24); var m: i64 = v; var k: i64 = 0; if m == 0 { t[0]=48 as u8; k=1 } while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 13 14func main(argc: i64, argv: *i64) -> i64 { 15 g_p("=== nx_ad_emitter_registry_gate ===\n" as *u8) 16 // fresh registry (truncate) 17 let fd: i64 = sys_openat_wr(GT_PATH, 420); if fd >= 0 { sys_close(fd) } 18 var pass: i64 = 0; var tot: i64 = 0 19 20 // ---- T1: TEACH 3 emitters -> count + round-trip ---- 21 er_register(GT_PATH, "accent" as *u8, "monogram" as *u8, "Monogram" as *u8, "<svg><circle/><text>{INI}</text></svg>" as *u8) 22 er_register(GT_PATH, "accent" as *u8, "star" as *u8, "Star" as *u8, "<svg><path d='star'/></svg>" as *u8) 23 er_register(GT_PATH, "animation" as *u8, "pulse" as *u8, "Pulse" as *u8, "@keyframes nbp-{S}{}" as *u8) 24 let oid: *u8 = sys_mmap(64); let olbl: *u8 = sys_mmap(128); let otpl: *u8 = sys_mmap(2048) 25 let got: i64 = er_get(GT_PATH, "accent" as *u8, 1, oid, olbl, otpl) 26 tot = tot + 1; var ok1: i64 = 1 27 if er_count(GT_PATH, "accent" as *u8) != 2 { ok1 = 0 } 28 if er_count(GT_PATH, "animation" as *u8) != 1 { ok1 = 0 } 29 if er_present(GT_PATH, "accent" as *u8, "star" as *u8) != 1 { ok1 = 0 } 30 if got != 1 { ok1 = 0 } 31 if er_streq(oid, "star" as *u8) != 1 { ok1 = 0 } 32 if otpl[0] == (0 as u8) { ok1 = 0 } // template round-trips non-empty 33 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 teach: count=2/1, round-trip accent[1]=star with template\n" as *u8) } else { g_p("FAIL T1\n" as *u8) } 34 35 // ---- T2: idempotent re-teach (no duplicate) ---- 36 let r: i64 = er_register(GT_PATH, "accent" as *u8, "star" as *u8, "Star" as *u8, "<svg/>" as *u8) 37 tot = tot + 1; var ok2: i64 = 1 38 if r != 0 { ok2 = 0 } // already-known -> 0 39 if er_count(GT_PATH, "accent" as *u8) != 2 { ok2 = 0 } // count unchanged 40 if ok2 == 1 { pass = pass + 1; g_p("PASS T2 idempotent: re-teach returns 0, count unchanged\n" as *u8) } else { g_p("FAIL T2\n" as *u8) } 41 42 // ---- T3: GROW -> catalog increases ---- 43 er_register(GT_PATH, "accent" as *u8, "burst" as *u8, "Burst" as *u8, "<svg><path d='burst'/></svg>" as *u8) 44 er_register(GT_PATH, "theme" as *u8, "ocean" as *u8, "Ocean" as *u8, "#06283d|#eaf6ff|#1b98e0|#062033" as *u8) 45 tot = tot + 1; var ok3: i64 = 1 46 if er_count(GT_PATH, "accent" as *u8) != 3 { ok3 = 0 } 47 if er_count(GT_PATH, "theme" as *u8) != 1 { ok3 = 0 } 48 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 grow: accent 2->3, theme 0->1 (builder learned new emitters)\n" as *u8) } else { g_p("FAIL T3\n" as *u8) } 49 50 // ---- T4: SAFETY invariant -- a script/fetch template is rejected; a clean one accepted (detector fires) ---- 51 tot = tot + 1; var ok4: i64 = 1 52 if er_template_safe("<svg><circle fill='#fff'/></svg>" as *u8) != 1 { ok4 = 0 } // clean -> safe 53 if er_template_safe("<script>evil()</script>" as *u8) != 0 { ok4 = 0 } // script -> rejected 54 if er_template_safe("<img src='//cdn/x.png'>" as *u8) != 0 { ok4 = 0 } // external fetch -> rejected 55 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 safety: clean template safe; <script and src= both REJECTED\n" as *u8) } else { g_p("FAIL T4\n" as *u8) } 56 57 // ---- T5: TEMPLATE ROUND-TRIP (mutation-derived, debt 1785604588) ---- 58 // nx_gate_mutation_probe scored this pair 3/4 with ONE SURVIVING MUTANT at 59 // nx_ad_emitter_registry.nx:56 -- ` + ` -> ` - ` inside er_register's TEMPLATE copy loop, which 60 // corrupts the output offset while writing field 3. It survived because T1-T4 verify kind, id, 61 // label, counts and template SAFETY, but never read a stored template BACK. So the registry could 62 // persist a mangled template and every tooth still passed. 63 // A round-trip is the tooth that cannot be satisfied by a broken writer: register a known template, 64 // read it back through er_lookup, and require BYTE EQUALITY. This is the negative control R4 asks 65 // for -- it fails the moment that offset arithmetic is wrong. 66 tot = tot + 1; var ok5: i64 = 1 67 let rtpl: *u8 = "#010203|#040506|#070809|#0a0b0c" as *u8 68 er_register(GT_PATH, "rt" as *u8, "probe" as *u8, "RT" as *u8, rtpl) 69 let rgot: *u8 = sys_mmap(2048) 70 if er_lookup(GT_PATH, "rt" as *u8, "probe" as *u8, rgot) != 1 { ok5 = 0 } 71 if er_streq(rgot, rtpl) != 1 { ok5 = 0 } 72 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 template round-trip is BYTE-EXACT through er_register -> er_lookup\n" as *u8) } 73 if ok5 == 0 { g_p("FAIL T5 template round-trip -- stored template does not match what was registered\n" as *u8) } 74 75 g_p("nx_ad_emitter_registry_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 76 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 77 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 78 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 79 let ctr__dry: *i64 = gv_ctr() 80 ctr__dry[0] = pass 81 ctr__dry[1] = tot 82 let rc__dry: i64 = gv_verdict("AD-EMITTER-REGISTRY-GATE" as *u8, ctr__dry, "teach -> idempotent -> grow -> sovereign-safe)" as *u8) 83 sys_exit(rc__dry) 84 return rc__dry 85}