code wiki / _hdl_build / nx_uigen_refine_gate.nx

nx_uigen_refine_gate.nx source

↩ module page · 142 lines · 6869 B

1import "nx_gate_gn.nx" 2// nx_uigen_refine_gate.nx -- MEASURES the ITERATIVE-REFINE rung: spec-delta commands mutate a design and 3// re-emit deterministically. The refine metric pair is TARGETED CHANGE (the asked-for aspect changed) + 4// PRESERVATION (everything else stayed) -- checked on the emitted BYTES. Rule-based analog of the v0 chat 5// loop ("add a pricing table", "make it warmer"): feedback in, changed valid design out, nothing else broken. 6// expect_exit: 0 license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_uigen_l3.nx" 9 10func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func litlen(lit: *u8) -> i64 { var n: i64=0; while lit[n]!=(0 as u8){n=n+1} return n } 12func idxof(buf: *u8, len: i64, lit: *u8) -> i64 { 13 let ll: i64=litlen(lit) 14 if ll==0 { return 0-1 } 15 var i: i64=0 16 while i+ll<=len { var j: i64=0; var ok: i64=1; while j<ll { if buf[i+j]!=lit[j] { ok=0; j=ll } else { j=j+1 } } if ok==1 { return i } i=i+1 } 17 return 0-1 18} 19func has(buf: *u8, len: i64, lit: *u8) -> i64 { if idxof(buf,len,lit)>=0 { return 1 } return 0 } 20// build "rgb(r,g,b)" for hsl(h,s,l) into dst (returns dst for convenience) 21func rgbstr(dst: *u8, h: i64, s: i64, l: i64) -> *u8 { 22 let n: i64 = l1_rgb(dst, 0, h, s, l) 23 dst[n] = 0 as u8 24 return dst 25} 26func craftok(buf: *u8, len: i64) -> i64 { 27 if has(buf,len,"var(--nx-" as *u8)==0 { return 0 } 28 if has(buf,len,"skip-link" as *u8)==0 { return 0 } 29 return 1 30} 31 32func main() -> i64 { 33 let cap: i64 = 524288 34 let buf: *u8 = sys_mmap(cap) 35 let spec: *i64 = sys_mmap(128) as *i64 36 let cstr: *u8 = sys_mmap(64) 37 var pass: i64 = 0 38 var total: i64 = 0 39 40 // handcrafted baseline: hue 208 blue, analogous, mid radius, mid density, 3 middles [features, split, faq] 41 spec[0]=208; spec[1]=0; spec[2]=1; spec[3]=1; spec[4]=3 42 spec[5]=0; spec[6]=1; spec[7]=5 43 spec[10]=0 44 45 gp("=== NX-UIGEN-REFINE GATE -- spec-delta refine: targeted change + preservation, on the emitted bytes ===\n" as *u8) 46 var off: i64 = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 47 gp(" baseline: bytes=" as *u8); gn(off) 48 var ok: i64 = 1 49 if has(buf,off,"class=\"sec faqband\"" as *u8)==0 { ok=0 } 50 if has(buf,off,"class=\"sec priceband\"" as *u8)==1 { ok=0 } 51 if has(buf,off,rgbstr(cstr,208,62,46))==0 { ok=0 } 52 if craftok(buf,off)==0 { ok=0 } 53 total=total+1; pass=pass+ok 54 if ok==1 { gp(" [PASS] faq present, no pricing, hue-208 primary, craft\n" as *u8) } else { gp(" [FAIL] baseline\n" as *u8) } 55 56 // 1) add pricing -> pricing appears BEFORE faq (grammar rule on insert); features+split preserved 57 var ap: i64 = l3_refine(spec, "add pricing" as *u8) 58 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 59 ok = 1 60 if ap != 1 { ok=0 } 61 let pat: i64 = idxof(buf,off,"class=\"sec priceband\"" as *u8) 62 let fat: i64 = idxof(buf,off,"class=\"sec faqband\"" as *u8) 63 if pat < 0 { ok=0 } 64 if fat < 0 { ok=0 } 65 if pat >= 0 { if fat >= 0 { if fat < pat { ok=0 } } } 66 if has(buf,off,"class=\"grid3\"" as *u8)==0 { ok=0 } 67 if has(buf,off,"class=\"sec splitband\"" as *u8)==0 { ok=0 } 68 if spec[4] != 4 { ok=0 } 69 if craftok(buf,off)==0 { ok=0 } 70 total=total+1; pass=pass+ok 71 if ok==1 { gp(" [PASS] add pricing: inserted before faq, others preserved, nmid=4\n" as *u8) } else { gp(" [FAIL] add pricing\n" as *u8) } 72 73 // 2) hue 300 -> palette changes to 300; old 208 primary gone; sections untouched 74 ap = l3_refine(spec, "hue 300" as *u8) 75 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 76 ok = 1 77 if ap != 1 { ok=0 } 78 if has(buf,off,rgbstr(cstr,300,62,46))==0 { ok=0 } 79 if has(buf,off,rgbstr(cstr,208,62,46))==1 { ok=0 } 80 if has(buf,off,"class=\"sec priceband\"" as *u8)==0 { ok=0 } 81 if has(buf,off,"class=\"sec faqband\"" as *u8)==0 { ok=0 } 82 total=total+1; pass=pass+ok 83 if ok==1 { gp(" [PASS] hue 300: palette retargeted, sections preserved\n" as *u8) } else { gp(" [FAIL] hue 300\n" as *u8) } 84 85 // 3) accent complement -> accent becomes hue+180 = 120 (light accent tone s72 l50) 86 ap = l3_refine(spec, "accent complement" as *u8) 87 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 88 ok = 1 89 if ap != 1 { ok=0 } 90 if has(buf,off,rgbstr(cstr,120,72,50))==0 { ok=0 } 91 total=total+1; pass=pass+ok 92 if ok==1 { gp(" [PASS] accent complement: accent at hue 120\n" as *u8) } else { gp(" [FAIL] accent complement\n" as *u8) } 93 94 // 4) compact -> section rhythm tightens to 56px 95 ap = l3_refine(spec, "compact" as *u8) 96 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 97 ok = 1 98 if ap != 1 { ok=0 } 99 if has(buf,off,".sec{padding:56px 0}" as *u8)==0 { ok=0 } 100 total=total+1; pass=pass+ok 101 if ok==1 { gp(" [PASS] compact: rhythm 56px\n" as *u8) } else { gp(" [FAIL] compact\n" as *u8) } 102 103 // 5) rounder -> radius 14 -> 26 104 ap = l3_refine(spec, "rounder" as *u8) 105 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 106 ok = 1 107 if ap != 1 { ok=0 } 108 if has(buf,off,"border-radius:26px}" as *u8)==0 { ok=0 } 109 total=total+1; pass=pass+ok 110 if ok==1 { gp(" [PASS] rounder: radius 26px\n" as *u8) } else { gp(" [FAIL] rounder\n" as *u8) } 111 112 // 6) drop pricing -> gone again, faq stays, nmid back to 3 113 ap = l3_refine(spec, "drop pricing" as *u8) 114 off = l3_emit_from_spec(buf, 0, spec, "Nova" as *u8, "Nishi" as *u8) 115 ok = 1 116 if ap != 1 { ok=0 } 117 if has(buf,off,"class=\"sec priceband\"" as *u8)==1 { ok=0 } 118 if has(buf,off,"class=\"sec faqband\"" as *u8)==0 { ok=0 } 119 if spec[4] != 3 { ok=0 } 120 if craftok(buf,off)==0 { ok=0 } 121 total=total+1; pass=pass+ok 122 if ok==1 { gp(" [PASS] drop pricing: removed, faq preserved, nmid=3\n" as *u8) } else { gp(" [FAIL] drop pricing\n" as *u8) } 123 124 // 7) grammar refusals: dropping below 3 middles / adding a duplicate must be REFUSED 125 var r1: i64 = l3_refine(spec, "drop faq" as *u8) 126 var r2: i64 = l3_refine(spec, "add split" as *u8) 127 ok = 1 128 if r1 != 0 { ok=0 } 129 if r2 != 0 { ok=0 } 130 total=total+1; pass=pass+ok 131 if ok==1 { gp(" [PASS] refusals: below-min drop refused, duplicate add refused\n" as *u8) } else { gp(" [FAIL] refusals\n" as *u8) } 132 133 gp("\n-- REFINE MEASUREMENT: " as *u8); gn(pass); gp("/" as *u8); gn(total); gp(" (targeted change + preservation + grammar-guarded)\n" as *u8) 134 if pass == total { 135 gp("=== UIGEN-REFINE verdict=GREEN: feedback commands change EXACTLY the asked-for aspect, preserve the rest,\n" as *u8) 136 gp("and the grammar refuses edits that would break validity. Iterative-refine (rule-based) is real. \n" as *u8) 137 sys_exit(0); return 0 138 } 139 gp("=== UIGEN-REFINE verdict=RED\n" as *u8) 140 sys_exit(1) 141 return 1 142}